How to return ‘own’ 404 custom page

asp.nethttp-status-code-404

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

Best Answer

<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