为什么我的服务器在 GET 预检请求上响应 Method Not Allowed 并说 Allow Delete?
Why is my server responding Method Not Allowed on GET preflight request and saying Allow Delete?
我有一个 .Net Core 3.1 web API 设置为允许所有方法用于我的测试目的。
当我在特定端点上 运行 一个 HTTP GET 请求时,我得到 405 Method Not Allowed 并且响应 headers 说 Allow: DELETE。
此控制器和我的其他控制器中的所有其他端点均按预期工作。
这是我的控制器代码:
[Authorize]
[Produces("application/json")]
[Route("api/User")]
public class UserAccountController : ControllerBase
{
[HttpGet]
[AllowAnonymous]
[Route("ConfirmAccountEmail")]
protected override async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...
}
[HttpDelete("{id}")]
[Route("DeleteUserAccount")]
[Authorize(Roles = AdminToken)]
public override async Task<IActionResult> DeleteUserAccount([FromRoute] int id)
{
// user delete code here...
}
}
在我的 Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
{
app.UseRouting();
app.UseCors(builder =>
{
builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod(); // just for testing
});
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoint(e =>
{
e.MapControllers();
));
}
这是我的请求(来自 Postman)的样子
这是我从服务器返回的响应 headers
我尝试将路由和 HTTP 类型更改为 POST(并将参数作为 form-data 传递)。
我尝试删除那个 Delete 端点,在这种情况下我得到 404 Not Found。所以这让我相信,出于某种原因,控制器映射正在为我的其他路由绑定一个路由到 delete 方法,但我不明白为什么。
我觉得我遗漏了一些明显的东西,但我现在不知道那是什么。
感谢您的帮助!
通常您会收到 CORS,但根本问题与 CORS 规则无关。我建议试试这个
[Authorize]
[Produces("application/json")]
[Route("api/User")]
public class UserAccountController : ControllerBase
{
[HttpGet]
[AllowAnonymous]
protected override async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...
}
[HttpDelete("{id}")]
[Authorize(Roles = AdminToken)]
public override async Task<IActionResult> DeleteUserAccount([FromRoute] int id)
{
// user delete code here...
}
}
即从操作方法中删除 Route
属性,然后像这样测试端点
GET https://localhost:4000/api/User
和 DELETE https://localhost:4000/api/User/1234
另一种选择是删除 HTTP 动词属性
[Authorize]
[Produces("application/json")]
[Route("api/User")]
public class UserAccountController : ControllerBase
{
[AllowAnonymous]
[Route("ConfirmAccountEmail")]
protected override async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...
}
[Route("DeleteUserAccount/{id}")]
[Authorize(Roles = AdminToken)]
public override async Task<IActionResult> DeleteUserAccount([FromRoute] int id)
{
// user delete code here...
}
}
然后像这样调用端点 GET https://localhost:4000/api/UserConfirmAccountEmail
和 DELETE https://localhost:4000/api/DeleteUserAccount/123
When I run an HTTP GET request on a specific endpoint, I get 405 Method Not Allowed and the response headers say Allow: DELETE.
关于“动作定义”的文档,您可以找到:
Public 控制器上的方法,具有 NonAction 属性的方法除外,都是操作。
请尝试将您的 ConfirmAccountEmail
方法中的 protected
修改为 public
,如下所示。
public async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...
我有一个 .Net Core 3.1 web API 设置为允许所有方法用于我的测试目的。 当我在特定端点上 运行 一个 HTTP GET 请求时,我得到 405 Method Not Allowed 并且响应 headers 说 Allow: DELETE。
此控制器和我的其他控制器中的所有其他端点均按预期工作。
这是我的控制器代码:
[Authorize]
[Produces("application/json")]
[Route("api/User")]
public class UserAccountController : ControllerBase
{
[HttpGet]
[AllowAnonymous]
[Route("ConfirmAccountEmail")]
protected override async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...
}
[HttpDelete("{id}")]
[Route("DeleteUserAccount")]
[Authorize(Roles = AdminToken)]
public override async Task<IActionResult> DeleteUserAccount([FromRoute] int id)
{
// user delete code here...
}
}
在我的 Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
{
app.UseRouting();
app.UseCors(builder =>
{
builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod(); // just for testing
});
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoint(e =>
{
e.MapControllers();
));
}
这是我的请求(来自 Postman)的样子
这是我从服务器返回的响应 headers
我尝试将路由和 HTTP 类型更改为 POST(并将参数作为 form-data 传递)。
我尝试删除那个 Delete 端点,在这种情况下我得到 404 Not Found。所以这让我相信,出于某种原因,控制器映射正在为我的其他路由绑定一个路由到 delete 方法,但我不明白为什么。
我觉得我遗漏了一些明显的东西,但我现在不知道那是什么。
感谢您的帮助!
通常您会收到 CORS,但根本问题与 CORS 规则无关。我建议试试这个
[Authorize]
[Produces("application/json")]
[Route("api/User")]
public class UserAccountController : ControllerBase
{
[HttpGet]
[AllowAnonymous]
protected override async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...
}
[HttpDelete("{id}")]
[Authorize(Roles = AdminToken)]
public override async Task<IActionResult> DeleteUserAccount([FromRoute] int id)
{
// user delete code here...
}
}
即从操作方法中删除 Route
属性,然后像这样测试端点
GET https://localhost:4000/api/User
和 DELETE https://localhost:4000/api/User/1234
另一种选择是删除 HTTP 动词属性
[Authorize]
[Produces("application/json")]
[Route("api/User")]
public class UserAccountController : ControllerBase
{
[AllowAnonymous]
[Route("ConfirmAccountEmail")]
protected override async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...
}
[Route("DeleteUserAccount/{id}")]
[Authorize(Roles = AdminToken)]
public override async Task<IActionResult> DeleteUserAccount([FromRoute] int id)
{
// user delete code here...
}
}
然后像这样调用端点 GET https://localhost:4000/api/UserConfirmAccountEmail
和 DELETE https://localhost:4000/api/DeleteUserAccount/123
When I run an HTTP GET request on a specific endpoint, I get 405 Method Not Allowed and the response headers say Allow: DELETE.
关于“动作定义”的文档,您可以找到:
Public 控制器上的方法,具有 NonAction 属性的方法除外,都是操作。
请尝试将您的 ConfirmAccountEmail
方法中的 protected
修改为 public
,如下所示。
public async Task<IActionResult> ConfirmAccountEmail(string token, string email)
{
// confirm email code here...