gpt4 book ai didi

.net-core - .net Core 3.0 预览版 2 - [406 : Not Acceptable]

转载 作者:行者123 更新时间:2023-12-04 06:43:33 25 4
gpt4 key购买 nike

我基于 Microsoft 文档使用 Microsoft 2.2 构建了一个 Web API。
一切都按预期进行。

更新到 .net Core 3.0 Preview 2 后,所有端点都返回 [406: Not Acceptable]。

缺少一些东西,但到目前为止我无法理解缺少什么。以下代码与 .net core 2.2 相同。

程序.cs

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
//.ConfigureKestrel((context, options) => { });
}
}

Startup.cs
public class Startup
{

public void ConfigureServices(IServiceCollection services)
{
// Dependency Injection - Services
services.AddSingleton<IXXXService, XXXService>();

services.AddMvc();

services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder => builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod());
});

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.EnableAnnotations();
});
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
app.UseCors("AllowAll");

app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API"); });
}
}

XXXController.cs
[ApiController]
[Route("api/xxx")]
public class XXXController : BaseController
{
private readonly IXXXService _service;

public XXXController(IXXXService service)
{
_service = service;
}

[HttpGet]
[Produces("application/json", Type = typeof(PagedDto<XXX>))]
[SwaggerOperation(
Summary = "Gets all the XXX",
Description = "Requires admin privileges",
OperationId = "GetAllXXX",
Tags = new[] {"XXX"}
)]
public IActionResult GetAllXXX([FromQuery] PagingDto pagging)
{
return Ok(_service.GetAllXXX(pagging));
}
}

最佳答案

我在 Microsoft github 上发布了这个问题,这是他们的回应:

You have to install the nuget package Microsoft.AspNetCore.Mvc.NewtonsoftJson
In your startup.cs file, add the JsonFormatter like this :
services.AddMvc().AddNewtonsoftJson().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
通过此更改,一切都按预期进行。

关于.net-core - .net Core 3.0 预览版 2 - [406 : Not Acceptable],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54772840/

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