Redirect to Error Page in ASP.NET

 

In Asp.Net we can redirect to specific page whenever some exception happens in application. Here we discuss about different ways to redirect to Error page on exception.

 

We can redirect to error page on page level. That means we can specify Error page if exception occurs in specific page. We have to provide value for ErrorPage attribute in Page tag as shown below.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"

      Inherits="AspNetErrorHandling.WebForm1"   ErrorPage="~/Error.aspx"  

 %>

 

If you want we can mention Error page to whole application in Web.config file. If we mention ErrorPage in Web.config, whenever some exception occurs in all pages of application it redirects to that ErrorPage. We have to provide ErrorPage in Web.config as shown below.

 

<?xml version="1.0"?>

<configuration>

    <system.web>

        <compilation debug="true" targetFramework="4.0" />

      <customErrors mode="On" defaultRedirect="Error.aspx">

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

      </customErrors>     

    </system.web>

</configuration>

                                                                                     

                                                                                                               AspNetErrorHandling.zip (14.67 kb)