本文介绍了NotFound()返回200而不是404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在坚持这一点,无法弄清楚为什么会发生这种情况.

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

我在.Net Core中使用MVC控制器返回NotFound()"404"响应.但是,如果我使用console.log响应,则客户端(使用角度)会显示此信息...

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...

返回NotFound()会返回错误代码200而不是预期的404是什么原因吗?

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

这是我的控制器GET.

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!

为清楚起见,对于此实例,假设输入为null.我正在测试的是击中NotFound()而不是返回OK(input).已经设置了断点,但确实击中了NotFound(),但仍返回200的响应代码.

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.

标题-

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

推荐答案

我问了类似的问题并收到了某种答案...

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

解决方案Redirect("~/404.html");返回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");
        }
    }
}

这确实返回404.

这篇关于NotFound()返回200而不是404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:39
查看更多