Can we handle the error and redirect to some pages using web.config?

Answer Posted / nikhiul saxena

Yes, 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:
< customErrors mode="RemoteOnly" defaultRedirect="Customerror.aspx" >
< error statusCode="404" redirect="Notfound.aspx" / >
< /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
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim str As System.Text.StringBuilder
str.Append("hi") ' Error Line as str is not instantiated
Response.Write(str.ToString)
End Sub
[Web.Config]
< customErrors mode="On" defaultRedirect="Error.aspx"/ >
' a simple redirect will take the user to Error.aspx [user defined] error file.
< customErrors mode="RemoteOnly" defaultRedirect="Customerror.aspx" >
< error statusCode="404" redirect="Notfound.aspx" / >
< /customErrors >
'This will take the user to NotFound.aspx defined in IIS.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you store a value in viewstate and retrieve them?

762


If Instancing = Single use for ActiveX Exe, how will this be executed if there are 2 consecutive client requests ?

2196


Can you nest updatepanel within each other?

735


What are the advantages of asp.net?

717


Can you explain how ASP.NET application life cycle and page life cycle events fire?

773


Which is the parent class of the web server control?

763


How does asp.net work?

719


Less than one page, how many windows will you be able to maintain?

756


Differentiate strong typing and weak typing

711


how to retrieve property settings from xml .config file.

716


How to create events for a control?

750


What is the difference between a page theme and a global theme?

729


Please briefly explain the usage of global.asax?

707


What is simple data binding?

709


How should I destroy my objects in asp.net?

776