Asp.net

Blazor WebAssembly:不支持提供的 ContentType;

  • January 22, 2021

我今天通過以下命令行安裝了 Blazor WebAssembly 範例項目 (WeatherForecast):

dotnet new blazorwasm --hosted -o ProjectName

我在調試模式下通過 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 

Chrome 顯示以下請求標頭:

Blazor WebAssembly 請求標頭

上下文資訊:

  • 鉻:V83.0.4103.61(64位)
  • Blazor:V3.2.0
  • 視覺工作室:V16.6.0
  • 點網V3.1.300
  • 點網SDK:V3.1.300

這很奇怪,因為這個範例項目的全新安裝昨天沒有問題。

如果將客戶端項目指定為啟動項目,則可能會在 Web 程序集模板項目的 ASP.NET 託管版本中發生這種情況。確保將伺服器項目設置為啟動項目。

Blazor WebAssembly - 伺服器 - 啟動項目

儘管對於獲得“200 Ok”響應的 OP 來說並非如此,但如果您的 API 方法返回 null(我的方法是拋出 DB 異常並返回 null 作為備份),也會發生這種情況。

我有“ 204 No Content ”作為響應,並且“不支持提供的 ContentType;支持的類型是 ‘application/json’ 和結構化語法後綴 ‘application/+json’。” 作為客戶端的錯誤。

我想 null 看起來不像 json :-)

引用自:https://stackoverflow.com/questions/62082133