views:

65

answers:

2

I know how to redirect pages with Rewrite. But when there is a 404 page how would i display my own custom 404 page (i know i can rewrite it) and say this is in fact a 404 and not just a plain rewritten page?

-edit-

How i can SET the error code instead of asking the server? if the user goes to /user/invaliduser/ i would like to send a 403 or 404 but my rewrite would send it to a valid page to display user info so i would like to know how to say doesnt exist rather then empty fields in a page.

+1  A: 

U can do a 404.html and redirect the 404 error in the web.config file to your own file

<error statusCode="404" redirect="YOURFILE.htm" />
Dejan.S
+2  A: 

To be more flexible then strict 404.html follow my example. In Global.asax.cs override:

public void Application_Error(Object sender, EventArgs e)
{
        if (HttpContext.Current.Request.IsLocal) return;

        Exception ex = Server.GetLastError();
        HttpException httpEx = ex as HttpException;
        string errorUrl;
        if (httpEx != null && httpEx.GetHttpCode() == 403)
            errorUrl = "~/error/403.aspx";
        else if (httpEx != null && httpEx.GetHttpCode() == 404)
            errorUrl = "~/error/404.aspx";
        else
            errorUrl = "~/error/500.aspx";
        Server.Transfer(errorUrl);
}

For example my 404.aspx contains form to send feedback, 403.aspx - exposes more interesting items for user, and 500 sends alert to Admin.

Dewfy
interesting. Do you know how i can SET the error code instead of asking the server? if the user goto /user/invaliduser/ i would like to send a 403 or 404 but my rewrite would send it to a valid page to display user info so i would like to know how to say doesnt exist rather then empty fields in a page.
acidzombie24
@acidzombie24 You have 2 ways. Throw HttpException(403) or any other code. Or explicitly set HttpContext.Current.Response.StatusCode. Last way allows even override previous error even to success (200)
Dewfy