Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET customErrors with mode=remoteOnly and global.asax handling exceptions

I have custom errors set in the web config file as follows:

<customErrors mode="RemoteOnly" defaultRedirect="GenericError.aspx" />

Fine and dandy... I like that mode="RemoteOnly" facilitates development...

For unhandled exceptions, I have in global.asax:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("GenericError.aspx")
End Sub

However, unhandled exceptions are going to the generic error page instead of the informative yellow screen of death preferred by developers. I can comment out the redirect in global.asax, but then I need to remember to change it for the production environment. Is there a way I can check in Application_Error whether I am remote or not to determine whether to redirect?

like image 968
harrije Avatar asked Jul 19 '10 16:07

harrije


People also ask

What is customErrors mode RemoteOnly?

RemoteOnly will give end users the custom error message and local usrs the standard asp.net error page. If your developers use a local web server for development you have both in one. Another approach is to set the <customErrors> to Off on development servers and set it to On in the production environment.

How do you handle exceptions in global ASAX?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.

Where do you put customErrors mode off?

The customErrors tag must be placed within tags. Create a <customErrors> tag within a "web. config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

How does MVC handle application error in global ASAX?

Perhaps a better way of handling errors in MVC is to apply the HandleError attribute to your controller or action and update the Shared/Error. aspx file to do what you want. The Model object on that page includes an Exception property as well as ControllerName and ActionName.


1 Answers

You do not need the Response.Redirect in the Global.asax. It duplicates the behaviour of the <customErrors> tag. RemoteOnly will give end users the custom error message and local usrs the standard asp.net error page. If your developers use a local web server for development you have both in one.

Another approach is to set the <customErrors> to Off on development servers and set it to On in the production environment. There are usually many items in the web.config that need changing so it is no more of a hardship.

like image 138
Philip Smith Avatar answered Sep 28 '22 02:09

Philip Smith