【发布时间】:2018-12-19 23:23:54
【问题描述】:
我正在尝试捕获错误 405 以启动个性化响应,但我做不到。当我调用该方法时,我得到一个 CORS 问题的一般错误
//Startup.cs
servicesCollection.AddCors(x =>
{
x.AddPolicy(CORS.AllowPutMethod,
policyBuilder =>
{
policyBuilder.WithOrigins("http://localhost:4200")
.WithMethods(HttpMethods.Put).AllowAnyHeader();
});
x.AddPolicy(CORS.AllowPostMethod,
policyBuilder =>
{
policyBuilder.WithOrigins("http://localhost:4200")
.WithMethods(HttpMethods.Post).AllowAnyHeader();
});
});
public static class CORS
{
public const string AllowPutMethod = nameof(AllowPutMethod);
public const string AllowPostMethod = nameof(AllowPostMethod);
}
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
// PUT: api/User/5
[HttpPut("{id}")]
[EnableCors(CORS.AllowPostMethod)] <=== ERROR HERE!!!
public void Put(int id, UserDTO currentUser)
{
}
}
【问题讨论】:
标签: asp.net-core asp.net-core-2.0 asp.net-core-webapi asp.net-core-2.1