将客户端 Blazor 添加到现有 Asp.Net Core 3.1 应用

Add client-side Blazor to existing Asp.Net Core 3.1 App

我正在尝试在现有 Asp.Net Core 3.1 应用程序的控制器视图中呈现客户端 Blazor 应用程序。

我开始关注 this guide 并根据模板创建了一个空应用程序。 我发现我需要的是:

  1. 这些 nuget 包:
    <PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.2.0-preview1.20073.1" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview1.20073.1" />
    
  2. Program.Main 中的这段代码:
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");
    await builder.Build().RunAsync();
    
  3. Blazor 应用 View 中的此 HTML 代码 'entry point':

    <app>Loading...</app>
    
    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss"></a>
    </div>
    <script src="_framework/blazor.webassembly.js"></script>
    
  4. .razor 组件:
    • App.razor
    • _Imports.razor
    • Shared/MainLayout.剃刀
    • Shared/NavMenu.剃刀

应该是这样,但我收到错误(如果需要可以添加完整的堆栈跟踪):

---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Microsoft.AspNetCore.Mvc.Razor, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

我在哪里可以找到那个 nuget 包版本?我需要程序集绑定吗? 我试过没有成功:

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.0" />

编辑

添加以下软件包解决了该问题:

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />

但是现在我有一个不同的错误:

C:\Program Files\dotnet\sdk.1.101\Sdks\Microsoft.NET.Sdk.Razor\build\netstandard2.0\Microsoft.NET.Sdk.Razor.Component.targets(106,5): error : rzc generate exited with code 1.

并且 Visual Studio (16.4.4) 在我打开任何 .razor 文件后立即崩溃

在您的项目中添加以下包:

<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.2.0-preview1.20073.1" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview1.20073.1" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="3.2.0-preview1.20073.1" />

在您的 Startup.Configure 方法中添加:

app.UseClientSideBlazorFiles<BlazorApp.Program>()
   .UseEndpoints(endpoints =>
   {
       endpoints.MapFallbackToClientSideBlazor<BlazorApp.Program>("blazor");
   });

Where "blazor" is the relative uri to your razor view hosting the blazor app.
and BlazorApp.Program the type of your blazor app Program

如果您希望在 2 个单独的项目中使用它,那么 Blazor 应用程序项目必须具有这些包:

<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.2.0-preview1.20073.1" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview1.20073.1" />

和服务器项目:

<PackageReference Include="Microsoft.AspNetCore.Blazor.Server" Version="3.2.0-preview1.20073.1" />