繁体   English   中英

从 NotFound() 取回不允许的 405 方法

[英]Getting back a 405 Method Not Allowed from NotFound()

In an ASP.NET Core 2.2 MVC app, I have a View that makes an Ajax request back to a controller, the request goes through and gets to the controller where a condition is met that should return a NotFoundResult (404):

$.ajax({
    type: "POST",
    url: "/My/AjaxAction",
    data: {
        name: name
    }
})

[HttpPost]
public async Task<IActionResult> AjaxAction(string name)
{
    if (/* condition */)
    {
        return NotFound();
    }

    //....
}

在测试这个时,我得到一个 405 Method Not Allowed 响应而不是 404。我调试并验证我正在点击NotFound()行。 但是 EndpointMiddleware 正在将响应更改为 405,我不知道为什么:

信息:Microsoft.AspNetCore.Mvc.StatusCodeResult[1] 正在执行 HttpStatusCodeResult,设置 HTTP 状态代码 404 信息:Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2] 执行动作 MyProject.MyController.AjaxAction (MyProject) in 2071.1794ms 信息:Microsoft .AspNetCore.Routing.EndpointMiddleware[1] 执行端点“MyProject.MyController.AjaxAction (MyProject)”信息:Microsoft.AspNetCore.Routing.EndpointMiddleware[0] 执行端点“405 HTTP 方法不支持”

在应用程序中使用 POST 的其他 Ajax 操作仍然可以正常工作,只是这一个是问题所在。

更新:如果我不满足NotFound()的条件,而是正常返回Ok() ,那么它应该返回正确的响应。 只有 404 被更改为 405。

这是关于 GitHub 的长时间讨论: https://github.com/aspnet/Mvc/issues/388

在简历中,您总是调用 url: /My/AjaxAction并且您希望有时它返回404 (此端点不存在),有时返回200 但它是同一条路线,因此对于您的 api 的消费者来说会有些混乱。 我相信这就是 Asp.Net 团队这样做的原因。

您可以进行一些更改来解决它。

一种选择是在 url 上添加名称参数,这样您的 url 将不会总是相同,您将能够返回 not found。

$.ajax({
    type: "POST",
    url: "/My/AjaxAction/"+name,
    data: {
        // other datainfo 
    }
})

[HttpPost]
public async Task<IActionResult> AjaxAction(string id) // Change name to id to asp.net route understand that this parameter is in your route and not on body.
{
    if (/* condition */)
    {
        return NotFound();
    }

    //....
}

其他选项是返回其他类型的 http 错误。 像400

$.ajax({
    type: "POST",
    url: "/My/AjaxAction",
    data: {
        name: name
    }
})

[HttpPost]
public async Task<IActionResult> AjaxAction(string name)
{
    if (/* condition */)
    {
        return BadRequest(); // You can also add an error message or an model;
    }

    //....
}

你的电话有问题。 您能否将 url 更改为“/My/AjaxAction/test”而不是“/My/AjaxAction”

$.ajax({
    type: "POST",
    url: "/My/AjaxAction/test",
    data: {
        name: name
    }
})

此外,您可以尝试在 HttpPost 中指定路由,例如:

[HttpPost("/My/AjaxAction")]
public async Task<IActionResult> AjaxAction(string id) // Change name to id to asp.net route understand that this parameter is in your route and not on body.
{
    if (/* condition */)
    {
        return NotFound();
    }

    //....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM