【发布时间】:2021-06-08 22:08:40
【问题描述】:
我创建了另一个与使用默认 Blazor WebAssembly 项目创建的默认 WeatherForestcastController 相同的控制器,并将控制器命名为“IncidentController”。默认 WeatherForestcastController 的唯一变化是名称:
[ApiController]
[Route("[controller]")]
public class **IncidentController** : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<**IncidentController**> logger;
public **IncidentController**(ILogger<**IncidentController**> logger)
{
this.logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
如果我通过 Postman (https://localhost:44337/Incident) 运行请求,我会得到预期的结果 - 五个随机 WeatherForecast 对象,就像 WeatherForecastController 一样。
然后,我创建了一个 razor 页面 Incidents.razor,它与 Blazor 项目创建的 FetchData.razor 页面相同。唯一的区别是我调用“事件”控制器而不是 WeatherForecastController。我只是显示差异:
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("**Incident**");
}
如果我访问 Incidents.razor 页面,我会收到以下异常:
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) <0x2ebafb0 + 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) <0x3069af0 + 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) <0x3048af8 + 0x0011c> in <filename unknown>:0
at BlazorAuthenticationTest.Client.Pages.Incidents.OnInitializedAsync () [0x00033] in C:\Users\scstoecker\source\repos\BlazorAuthenticationTest\BlazorAuthenticationTest\Client\Pages\Incidents.razor:43
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x2b9a930 + 0x0013a> in <filename unknown>:0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2dc2a30 + 0x000b6> in <filename unknown>:0
知道为什么只更改控制器名称会导致此 ContentType 异常吗?我已经清理并重建了解决方案。
【问题讨论】:
-
错误在 URL 中,内容类型消息是“HTML 不是 JSON”。那has been asked before
-
PostMan 中使用的确切(完整)URL 是什么?
-
我同意@HenkHolterman 所说的。一定有什么不同。我怀疑您使用的是使用 Auth 的 HTTP 客户端。控制器没有 [Authorize] 属性。