我根据Microsoft文档使用Microsoft 2.2构建Web API。 一切都按预期进行。
更新到.net Core 3.0 Preview 2之后,所有端点都返回[406:不可接受]。
有一些丢失的东西,但是到目前为止我还无法理解丢失的东西。以下代码与.net core 2.2上的代码相同。
Program.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));
}
}
答案 0 :(得分:4)
我在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);
有了此更改,一切都会按预期进行。