Handle the Error and Redirect to Some Pages using Web.Config

We can do this, but to handle errors, we must know the error codes, only then we can take the user to a proper error message page, else it may confuse the user.CustomErrors Configuration section in web.config file:

The default configuration is:
  1. <customErrors mode="RemoteOnly"  
  2.    defaultRedirect="Customerror.aspx" >  
  3.    <error statusCode="404" redirect="Notfound.aspx" / >  
  4. </customErrors >  
If mode is set to Off, custom error messages will be disabled. Users will receive detailed exception error messages. If mode is set to On, custom error messages will be enabled. If mode is set to RemoteOnly, then users will receive custom errors, but users accessing the site locally will receive detailed error messages.
Add an < error > tag for each error you want to handle. The error tag will redirect the user to the Notfound.aspx page when the site returns the 404 ?Page not found?
error.

[Example]

There is a page MainForm.aspx
  1. Private    
  2. Sub Page_Load(ByVal sender As System.ObjectByVal e As System.EventArgs)    
  3. Handles MyBase.Load    
  4. 'Put user code to initialize the    
  5.   
  6. Dim str As System.Text.StringBuilder  
  7. str.Append("hi"' Error Line as str is not instantiated  
  8. Response.Write(str.ToString)  
  9. End Sub  
[Web.Config]

< customErrors mode="On" defaultRedirect="Error.aspx"/ >
' a simple redirect will take the user to Error.aspx [user defined] error file.
  1. <customErrors mode="RemoteOnly" defaultRedirect="Customerror.aspx" >  
  2.    <error statusCode="404" redirect="Notfound.aspx" / >  
  3. </customErrors >  
  4. 'This will take the user to NotFound.aspx defined in IIS.  
Ebook Download
View all
Learn
View all