Blazor WebAssembly:不支持提供的 ContentType;

Sim*_*mon 11 asp.net asp.net-web-api blazor blazor-client-side blazor-webassembly

我今天通过以下命令行安装了 Blazor WebAssembly 示例项目 (WeatherForecast):

dotnet new blazorwasm --hosted -o ProjectName
Run Code Online (Sandbox Code Playgroud)

我在调试模式下通过 Visual Studio 2019 安装后直接启动了 SPA。浏览器是:Chrome。浏览器显示应用程序,但是当我选择导航菜单“获取数据”时,什么也没有发生。Chrome 显示 1 个错误:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: The provided ContentType is not supported; the supported types are 'application/json' and the structured syntax suffix 'application/+json'.
System.NotSupportedException: The provided ContentType is not supported; the supported types are 'application/json' and the structured syntax suffix 'application/+json'.
  at System.Net.Http.Json.HttpContentJsonExtensions.ValidateContent (System.Net.Http.HttpContent content) <0x2e87f38 + 0x0009a> in <filename unknown>:0 
  at System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync[T] (System.Net.Http.HttpContent content, System.Text.Json.JsonSerializerOptions options, System.Threading.CancellationToken cancellationToken) <0x2e87d30 + 0x00006> in <filename unknown>:0 
  at System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsyncCore[T] (System.Threading.Tasks.Task`1[TResult] taskResponse, System.Text.Json.JsonSerializerOptions options, System.Threading.CancellationToken cancellationToken) <0x2e40838 + 0x0011c> in <filename unknown>:0 
  at BlazorIssue.Client.Pages.FetchData.OnInitializedAsync () [0x00033] in D:\Projects\BlazorIssue\Client\Pages\FetchData.razor:43 
  at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2b8cb98 + 0x0013a> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2dd8340 + 0x000b6> in <filename unknown>:0 
Run Code Online (Sandbox Code Playgroud)

Chrome 显示以下请求标头:

Blazor WebAssembly 请求标头

上下文信息:

  • 铬:V83.0.4103.61(64位)
  • Blazor:V3.2.0
  • 视觉工作室:V16.6.0
  • 点网 V3.1.300
  • 点网 SDK:V3.1.300

这很奇怪,因为这个示例项目的全新安装昨天没有问题。

Mik*_*ind 20

如果客户端项目被指定为启动项目,这可能发生在 Web 程序集模板项目的 ASP.NET 托管版本中。确保将服务器项目设置为启动项目。

Blazor WebAssembly - 服务器 - 启动项目