ScottSto
ScottSto

Reputation: 731

HttpClient request to controller returns "The provided ContentType is not supported..."

I have created another controller identical to the default WeatherForestcastController created with a default Blazor WebAssembly project, and have named the controller "IncidentController". The only changes from the default WeatherForestcastController are the names:

[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();
}

}

If I run the request through Postman (https://localhost:44337/Incident), I get the results I expect - five random WeatherForecast objects, just like the WeatherForecastController.

I then created a razor page, Incidents.razor, that is identical to the FetchData.razor page created by the Blazor project. The only difference is that I call the "Incident" controller instead of the WeatherForecastController. I am only displaying the difference:

protected override async Task OnInitializedAsync()
{
    forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("**Incident**");
}

If I access the Incidents.razor page, I receive the following exception:

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 

Any idea why just changing the controller name would cause this ContentType exception? I have cleaned and rebuilt the solution.

Upvotes: 1

Views: 157

Answers (2)

Iria
Iria

Reputation: 497

I know that you sorted the problem, but I will post another answer.

I had the same problem, but in my case, postman worked, but not my client, it was due to missing "application/json" when converting the object to httpContent that I needed to send

Upvotes: 0

ScottSto
ScottSto

Reputation: 731

I rebooted and the new controller worked. Hmm. I added a new controller, TestController, and tried again, and the same "Unhandled exception rendering component..." error appeared. I deleted the obj and bin directories and rebuilt, and the TestController worked. I suppose I will just need to redo that process if I see that particular error again.

Upvotes: 1

Related Questions