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



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

Answer / 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

More ASP.NET Interview Questions

In cache where dats is stored and how(file or object?)

2 Answers  


how can you bind data from dataset to XML file

2 Answers   LG Soft,


If 200 is for all successful operation then why do we have 201 response codes?

0 Answers  


Can a master page have more than one contentplaceholder?

0 Answers  


is it possible to access website from a remote place, without deploying it on web server?

1 Answers  






There is a login page that has two text boxes with required field validators on it. The page has a login and cancel button. How can we ensure that the click on the cancel button doesnt fire a validation event.

2 Answers   Proteans,


What are client activated objects and server activated objects?

0 Answers  


How many languages are supported by .NET at present time?

0 Answers  


What is the main differences between asp and asp.net?

0 Answers  


how to stop cookie in particular page using asp.net

2 Answers   CGI,


Explain advantages of caching?

0 Answers  


One of the properties of the Web Part has an incorrect format. Windows SharePoint Services cannot deserialize the Web Part. Check the format of the properties and try again. - this error is occured in Sharepoint 2007 while deploy the web part

1 Answers   WebInfo,


Categories