Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return 'own' 404 custom page?

Tags:

In case if error occurred on my web site I do the following:

        Server.Transfer("/error.aspx");

and that page has code:

protected void Page_Load(object sender, EventArgs e)
{
    ...

    Response.StatusCode = 404;
}

If I work on the localhost then together with 404 status returned for the page, page displays 'proper error description'.

Once I published the same code to the internet all pages with errors are still displayed with 404 status code, but the don't have the content. Instead, they have the standard 404 error message:

404 - File or directory not found.

if the line "Response.StatusCode = 404" commented out then the proper page is provided, but it has 200 status code.

Question: how to return user-friendly error page that in the same time has 404 error status code?

Any thoughts are welcome! Thanks a lot in advance!

P.S. ASP.NET 4.0

like image 352
Budda Avatar asked Apr 12 '12 02:04

Budda


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


1 Answers

<customErrors mode="On" defaultRedirect="~/Error/GenericErrorPage.aspx">

     <error statusCode="404" redirect="~/Error/404.aspx" />

</customErrors>

http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/aa479319.aspx

like image 78
adt Avatar answered Sep 21 '22 11:09

adt