我已经创建了另一个控制器,与使用默认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,这和FetchData是一样的。由Blazor项目创建的剃刀页面。唯一的区别是,我把它称为"事件"。控制器,而不是WeatherForecastController。我只显示差异:
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("**Incident**");
}
如果我访问事件。在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:UsersscstoeckersourcereposBlazorAuthenticationTestBlazorAuthenticationTestClientPagesIncidents.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异常吗?我已经清理并重建了解决方案。
我重新启动,新的控制器工作。嗯。我添加了一个新的控制器,TestController,然后再试一次,同样的"未处理的异常渲染组件……"错误出现。我删除了obj和bin目录并重新构建,TestController工作了。如果我再次看到这个错误,我想我只需要重做这个过程。
我知道你对问题进行了排序,但我会发布另一个答案。
我有同样的问题,但在我的情况下,邮差工作,但不是我的客户端,这是由于缺少"application/json"当将对象转换为我需要发送的httpContent时