1

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==?=
Alex Liosatos
  • 321
  • 1
  • 4
  • 15
  • 1
    Can you please try executing this request using Fiddler or Postman? – Federico Dipuma May 25 '17 at 14:58
  • It should work. Your code is correct. Go check the browser debugging tools by hitting F12 and select Netwrk tab. After show us the request headers. – CodeNotFound May 25 '17 at 15:00
  • You should add how you are calling that method, without that we cannot be sure of what you are seeing. – Camilo Terevinto May 25 '17 at 15:00
  • Basically if I enter a name of a cause that's not in my database I want it to return a 404 error so I can check for it client side. I'm calling the method by entering something like website/cause/dsadasdasdas – Alex Liosatos May 25 '17 at 15:06
  • Please do not use comments, add code and updates by editing your question. – Federico Dipuma May 25 '17 at 15:09
  • That seems to be a web page returned by Kestrel. Have you tried to copy and paste the url inside the browser? – Federico Dipuma May 25 '17 at 15:18
  • the data in the response appears to be my entire website in raw html. Im still confused why that would happen when sending a NotFound() response though, and why the error code would still be 200. – Alex Liosatos May 25 '17 at 15:25
  • @AlexLiosatos, Recently answered a similar question [here](https://stackoverflow.com/questions/44033517/how-to-throwing-404-on-bad-net-core-api-route/44159809#44159809) and it turns out that `routes.MapSpaFallbackRoute` was the cause of this. Check your startup and follow the OPs answer. – Nkosi May 25 '17 at 23:02
  • try to do like in this artcile https://stackoverflow.com/questions/49669766/mapspafallbackroute-on-startup-asp-net-core – probaumak _ Jan 20 '19 at 08:28

1 Answers1

0

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.

GUI Junkie
  • 539
  • 1
  • 17
  • 31