Returning a custom 404 page with 404 statuscode in MVC?
Asked Answered
I

2

9

I'm trying to return a 404 status code with my custom error page. However I keep getting 200 back, since the page does actual exists as far as the browser and server can tell.

What I have been trying so far is:

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

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

I have also tried to set the statuscode with in my action..

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

But that simply ends up with displaying the default Server error page (the gray-ish one with the red error message text)

Any ideas of how to get around this?

Inceptive answered 9/6, 2014 at 9:38 Comment(1)
It looks like your question may already have an answer here: #5635614Silber
C
11

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

public ActionResult NotFound()
{
    Response.StatusCode = 404; 
    Response.TrySkipIisCustomErrors = true; <---
    return View();
}
Camshaft answered 8/4, 2015 at 21:54 Comment(0)
H
0

Have you tried:

return HttpNotFound();

You can find more information here:

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

Hindman answered 10/2, 2015 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.