[Solved] Custom error page redirect not working after deployed in IIS

Last Reply 6 months ago By Sumeet

Posted 6 months ago

Sir,

In my web.config I have used customError tag to handle 404 with mode="ON".

Also added a global.asax and in Application_Error() method added below code: 

var serverError = Server.GetLastError() as HttpException;
if (serverError != null)
{
    if (serverError.GetHttpCode() == 404)
    {
        Server.ClearError();
        Server.Transfer("/Errors/404.aspx");
    }
}

But when I am publishing application to IIS and accessing, it does not handling 404. 

What is the best way to handle it?

You are viewing reply posted by: Sumeet 6 months ago.
Posted 6 months ago

This is the way i have achieved this:

In web.config file within <configuration></configuration> place below code and provide your own path of error page.

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse ="Replace">
        <remove statusCode="404" />
        <error statusCode="404" path="404.aspx" responseMode="Redirect" prefixLanguageFilePath="" />
        <remove statusCode="500" />
        <error statusCode="500" path="500.aspx" responseMode="Redirect" prefixLanguageFilePath="" />
        <remove statusCode="403" />
        <error statusCode="403" path="403.aspx" responseMode="Redirect" prefixLanguageFilePath="" />
    </httpErrors>
</system.webServer>