Returning a custom 404 page with 404 status in MVC?

I am trying to return a 404 status code with my custom error page. However, I keep getting 200 back since the page is really relevant as far as the browser and server can tell.

What I'm trying to do so far:

<customErrors mode="On" defaultRedirect="~/404" > <error statusCode="404" redirect="~/404"/> </customErrors> 

Where 404 is the route to the controller and the page not found action.

I also tried to set the status code in my action.

 public ActionResult PageNotFound(string locale) { Response.StatusCode = 404; return View(); } 

But it finishes with the fact that page is displayed by default the server error (gray-ish with a red error message text)

Any ideas on how to get around this?

+7
c # asp.net-mvc custom-error-pages
source share
2 answers

I had the same problem and the solution is posted here :

 public ActionResult NotFound() { Response.StatusCode = 404; Response.TrySkipIisCustomErrors = true; <--- return View(); } 
+7
source share

You tried:

 return HttpNotFound(); 

Here you can find more information:

https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpnotfound%28v=vs.118%29.aspx

0
source share

All Articles