NotFound() returns 200 instead of 404

主宰稳场 提交于 2020-06-03 06:19:59

问题


I've been stuck on this recently and can't figure out why this is happening.

I'm using an MVC Controller in .Net Core to return a NotFound() "404" response. However, client side (using angular) if I console.log the response, it shows this...

status:200
statusText:"OK"

Is there any reason why returning NotFound() would return an error code of 200 instead of the intended 404?

This is my Controller 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);
        }
    }

Any help would be appreciated! Thanks!

To be clear, for this instance assume input is null. What I'm testing is it hitting NotFound() not the return OK(input). Breakpoints have been set and it does hit the NotFound() but still returns the response code of 200.

Headers--

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 –

POSTMAN HEADERS

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==?=

回答1:


I have asked a similar question and received some kind of answer... NotFound() doesn't seem to work as expected

The solution Redirect("~/404.html"); returns 200.

However, there's another way.

// 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");
        }
    }
}

This does return 404.



来源:https://stackoverflow.com/questions/44183416/notfound-returns-200-instead-of-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!