Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a custom 404 page with 404 statuscode in MVC?

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?

like image 472
Inx51 Avatar asked Jun 09 '14 09:06

Inx51


People also ask

How do I return a 404 response code?

Open or create the . htaccess-file and enter the relative path to the error page. First though, you have to create the error page (404. html, for example) on the first level of your website (the root-directory).

Should a non existent page always return 404?

When a user requests a nonexistent URL on your website, you should return an individual error page that lets them know that the requested URL does not exist. You should also make sure that the server returns the correct HTTP status code “404“.


1 Answers

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

public ActionResult NotFound()
{
    Response.StatusCode = 404; 
    Response.TrySkipIisCustomErrors = true; <---
    return View();
}
like image 117
Maksim Vi. Avatar answered Oct 01 '22 04:10

Maksim Vi.