【问题标题】:NotFound() returns 200 instead of 404NotFound() 返回 200 而不是 404
【发布时间】:2017-10-26 06:37:09
【问题描述】:

我最近一直被这个问题困扰,无法弄清楚为什么会发生这种情况。

我在 .Net Core 中使用 MVC 控制器来返回 NotFound()“404”响应。 但是,客户端(使用角度)如果我 console.log 响应,它会显示这个......

状态:200
状态文本:“确定”

是否有任何理由返回 NotFound() 会返回错误代码 200 而不是预期的 404?

这是我的控制器 GET。

 // GET: api/cause/cause-name
    [HttpGet("{name}")]
    [AllowAnonymous]
    public IActionResult GetCauseByName(string name)
    {
        var input =  _service.GetCauseByName(name);

        if (input == null)
        {
            return NotFound();
        }
        else
        {
            return Ok(input);
        }
    }

任何帮助将不胜感激!谢谢!

为了清楚起见,在本例中假设输入为空。我正在测试的是它击中 NotFound() 而不是返回 OK(input)。已设置断点,它确实命中了 NotFound(),但仍返回响应代码 200。

标题--

GET /cause/dsdasdas 
HTTP/1.1 
Host: localhost:48373 
Connection: keep-alive 
Upgrade-Insecure-Requests: 1 
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/‌​webp,/;q=0.8 Accept-Encoding: gzip, deflate, sdch, br Accept-Language: en-US,en;q=0.8

HTTP/1.1 
200 OK 
Transfer-Encoding: chunked 
Content-Type: text/html; charset=utf-8 
Content-Encoding: gzip Vary: 
Accept-Encoding Server: Kestrel X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcaXR0ZW1wNVxEZXNrdG9wXFByb2plY3RGdW5kQX‌​BwXHNyY1xQcm9qZWN0Rn‌​VuZFxjYXVzZVxkc2Rhc2‌​Rhcw==?= X-Powered-By: ASP.NET Date: Thu, 25 May 2017 14:51:29 GMT –

邮差标题

Content-Encoding →gzip
Content-Type →text/html; charset=utf-8
Date →Thu, 25 May 2017 15:18:31 GMT
Server →Kestrel
Transfer-Encoding →chunked
Vary →Accept-Encoding
X-Powered-By →ASP.NET
X-SourceFiles →=?UTF-8?B?QzpcVXNlcnNcaXR0ZW1wNVxEZXNrdG9wXFByb2plY3RGdW5kQXBwXHNyY1xQcm9qZWN0RnVuZFxjYXVzZVxkc2Rhc2Rhcw==?=

【问题讨论】:

  • 您可以尝试使用 Fiddler 或 Postman 执行此请求吗?
  • 它应该可以工作。你的代码是正确的。按 F12 检查浏览器调试工具,然后选择“网络”选项卡。在向我们展示请求标头之后。
  • 您应该添加调用该方法的方式,否则我们无法确定您所看到的内容。
  • 基本上,如果我输入一个不在我的数据库中的原因的名称,我希望它返回一个 404 错误,以便我可以在客户端检查它。我通过输入类似 website/cause/dsadasdasdas 来调用该方法
  • 请不要使用 cmets,通过编辑您的问题添加代码和更新。

标签: c# asp.net-core-mvc


【解决方案1】:

我问了一个类似的问题并收到了一些答案...NotFound() doesn't seem to work as expected

解决方案Redirect("~/404.html"); 返回 200。

但是,还有另一种方法。

// Wherever you want to return your standard 404 page
return Redirect("Home/StatusCode?code=404");


public class HomeController : Controller
{
    // This method allows for other status codes as well
    public IActionResult StatusCode(int? code)
    {
        // This method is invoked by Startup.cs >>> app.UseStatusCodePagesWithReExecute("/Home/StatusCode", "?code={0}");
        if (code.HasValue)
        {
            // here is the trick
            this.HttpContext.Response.StatusCode = code.Value;
        }

        //return a static file.
        try
        {
            return File("~/" + code + ".html", "text/html");
        }
        catch (FileNotFoundException)
        {
            return Redirect("Home/StatusCode?code=404");
        }
    }
}

这确实返回 404。

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2010-09-25
    • 2016-02-10
    • 2019-10-29
    • 1970-01-01
    相关资源
    最近更新 更多