提供的ContentType不支持;支持的类型是blazor中的“ application / json”

问题描述

我在blazor webassemblyapp(客户端)中调用blazor serverapp(webwebapi),但是我遇到内容类型错误:

crit: 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) <0x32571f8 + 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) <0x3256ff0 + 0x00006> in <filename unknown>:0 
  at System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsyncCore[T] (System.Threading.Tasks.Task`1[TResult] taskResponse,System.Threading.CancellationToken cancellationToken) <0x321fd50 + 0x0011c> in <filename unknown>:0 
  at CrudBlazorClientApp.Pages.Index.Login () [0x00106] in C:\Users\FrontTech\source\repos\CrudBlazorServerApp\CrudBlazorClientApp\Pages\Index.razor:31 
  at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x3220cb0 + 0x000da> in <filename unknown>:0 
  at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x3156c98 + 0x000b6> in <filename unknown>:0 

EmpsController.cs

namespace CrudBlazorServerApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmpsController : ControllerBase
    {
        //here I create a custom login webapi 
        [HttpPost]
        [Consumes("application/json")]
        public Emp checkLogin(string username,string password)
        {
            Emp hasemp = _context.emps.Where(e => e.username == username & e.password == password).FirstOrDefault();
            return hasemp;
        }

请参阅上面的webapi响应

enter image description here

查看监视窗口

enter image description here

查看控制台日志

enter image description here

请求标头

enter image description here

响应

无法加载响应数据

Index.razor

@page "/"
@using CrudBlazorServerApp.Data
@inject HttpClient Http
@inject NavigationManager navigationManager
@*@inject CrudBlazorServerApp.Data.sqldbcontext _sqldbcontext*@
@using Newtonsoft.Json;
<div>
    UserName:<input id="txtusername" type="text" placeholder="Enter Your UserName" @bind="@username" /><br />
    Password:<input id="txtpassword" type="text" placeholder="Enter Your Password" @bind="@password" /><br />
    <button @onclick="Login">Log In</button>
</div>

@code{
    string username = "";
    string password = "";
    Emp empList = new Emp();
    protected async Task Login()
    {
        @if (username == "" || username == null && password == "" || password == null)
        {
            var data = new
            {
                message = "Enter Username ",};
        }
        else
        {
            var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("Accept","application/json");
            empList = await client.GetFromJsonAsync<Emp>("https://localhost:44333/api/emps/checkLogin?username=" + empList.username + "&password=" + empList.password);

            @if (username == empList.username || password == empList.password)
            {
                var data = new
                {
                    message = "Success",data = new { empList.username,empList.password }
                };

                navigationManager.NavigateTo("/registration");

            }
            else
            {
                var data = new
                {
                    message = "Username and Password incorrect ",};
                navigationManager.NavigateTo("/index");
            }
        }
    }
}

当我调试此行时会生成错误:

empList = await client.GetFromJsonAsync<Emp>("https://localhost:44333/api/emps/checkLogin?username=" + empList.username + "&password=" + empList.password);

并且emplist也为空

帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)