Custom Error Handling:
In ASP.NET, we can customize error message using Web.Config 'CustomError' element.
<customErrors defaultRedirect="URL" mode="On | Off | RemoteOnly">
<error statusCode="statuscode" redirect="URL"/>
</customErrors>
Attributes:
defaultRedirect:
This specifies default URL to redirect when the error occurs
mode :
This specifies whether custom errors are enabled, disabled or enabled only for remote client.
- On - Specifies custom error is enabled.
- Off - Specifies custom error is disabled.
- RemoteOnly - This is the default. Specifies custom error is enabled for remote client. Means custom error message will be only shown to remote users and generic error message will be to local host.
Error tag:
- statusCode - Specifies HTTP status code that will result in the redirection to the error page.
- redirect - Redirect to URL when error occurs
This error tag can be many. We can specify many error codes and custom redirect URL.
We will see one simple example which opens custom error page when page not found error occurs(404 error) and some exception occurs.
In web.config:
Specify like this...
<configuration>
<system. web>
<customErrors defaultRedirect="GeneralError.htm" mode="On">
<error statusCode="404" redirect="pagenotfound.htm"/>
</customErrors>
</system. web>
</configuration>
Join the conversation! Your thoughts help the community grow.