Thursday, August 5, 2010

HandleError Attibute in ASP.NET MVC.

I noticed HandleError attribute in ASP.NET MVC default templated sample and noticed mirosoft documentation states this Attribute use for handling an exception thrown by an action method. I was curious to see how this works, and so tried in a sample application, on which I modified some of the files on mvc default templated sample.

I was just trying to divide by zero to get an exception. and bad luck I got the "Yellow Screen of Death" instead of handling the error!!!!...

After some googling I found
1. Set Custom Error Mode to "On" will fix this issue- This will redirect to custom error page even in debug mode. (mm.. this is the case in ASP.NET as well, so i should have thought this first, but microsoft documentation was misleading I guess)
""
where Error.aspx location should be inside Views\Shared\ folder.
However, after setting custom error mode too, I still get the error message in the browser. However templated sample from microsoft worked well after setting the custom error mode!!!!.

Exception raised in my custom sample:-

Initially I couldn't find the reason for the exception, as I was getting Resource not found exception . i.e the HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable

However after indepth analysis, by throwing exception in the initial action I got the following exception in Default.aspx.cs, where the current path was routed.

Could not load type 'System.Web.Mvc.ViewPage'.

Noticed my Error.aspx page Inherits="System.Web.Mvc.ViewPage"

Reson for the issue in the custom sample
After comparing my templated project and googling I found that web.config in my Views folder is missing
I copied the web.config file from View folder of templated project to my custom project. I was amazed to see the reason for my error handling to work..

Finally I found,

It is not the web.config file inside the view that matters, it is because, the pageParserFilterType (System.Web.Mvc.ViewTypeParserFilter) needs to be loaded.
This can either be in global web.config or inside the views folder.


pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"

There is more to know on the ASP.NET MVC Error handling, as for any project, the error handling framework needs to be built before starting any application in MVC.

No comments: