gpt4 book ai didi

asp.net-mvc - Blazor HttpClient 3.2.0 Get 调用抛出异常,因为响应 header 内容类型与 GetFromJsonAsync 不兼容

转载 作者:行者123 更新时间:2023-12-04 09:43:21 29 4
gpt4 key购买 nike

我正在从 blazor 3.2.0 客户端调用 webapi:

 protected override async Task OnParametersSetAsync()
{
if (SelectedComplexId != Guid.Empty)
{
residents = await HttpClient.GetFromJsonAsync<List<ResidentDTO>>($"api/complex/residents/{SelectedComplexId.ToString()}");
}
}

正在抛出异常,因为 GetFromJsonAsync 需要 application/json 的内容类型 header 作为响应。

这是 api 操作方法:
[HttpGet("/residents/{complexId}")]
public async Task<IActionResult> GetResidents([FromBody] string complexId)
{
var complexclaim = new Claim("complex", complexId);
var complexUsers = await userManager.GetUsersForClaimAsync(complexclaim);
var residents = mapper.Map<List<ResidentDTO>>(complexUsers);
return Ok(residents);
}

这个 api 应该返回一个 json 格式的对象。但是检查响应头类型它仍然显示 text/html。我的印象是从 IActionResult OK(..) 返回一个 json 格式的对象

enter image description here

以下是异常详情:
Unhandled exception rendering component: The provided ContentType is not supported; the supported types are 'application/json' and the structured syntax suffix 'application/+json'.

和创业类
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));

services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();

services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

services.AddAuthentication()
.AddIdentityServerJwt();

services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

services.AddRazorPages();
services.Configure<EmailOptions>(Configuration.GetSection("EmailSettings"));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IMailJetEmailService, MailJetEmailService>();
services.AddTransient<IManagingAgentService, ManagingAgentService>();
services.AddTransient<IProfileService, ProfileService>();

}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}

最佳答案

我认为您将错误返回为 HTML 页面。调试您的 API 操作或更深入地查看响应的内容。它可能来自 app.UseDeveloperExceptionPage();
该错误很可能来自路由或授权的某些问题。
在 Kestrel 控制台打开的情况下运行也可能提供更多信息。

你可以更换

[HttpGet("/residents/{complexId}")]


[HttpGet("/api/residents/{complexId}")]

关于asp.net-mvc - Blazor HttpClient 3.2.0 Get 调用抛出异常,因为响应 header 内容类型与 GetFromJsonAsync 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62235662/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com