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
Is it possible to change the index of primary key on table?
How do I send an email message from my ASP.NET page?
Can you explain one critical mapping? Performance issue which one is better?
How do you declare static variable? What is its lifetime?
What is postback pixel?
What tags do you need to add within the asp:datagrid tags to bind columns manually? Give an example.
How do I open an ashx file in windows 7?
Elaborate differentiation between Cache and Application?
What is the difference between adding reference in solution explorer and adding references by using ?
What is globalization and localization in asp net?
What are session and cookies?
How can we inherit a static variable?
Which validator control you use if you need to make sure the values in two different controls matched?
Explain the differences between managed and unmanaged code?
What are the elements of a website?