Global Filters with ASP.Net MVC

Hi Friends,

In today’s discussion, we’ll quickly see that how global filters make our life simple and easy by handling the unfortunate errors @ every controller level. so, before jumping into discussion, let’s 1st understand that what is global filter. A global filter for example HandleError attribute is something which displays a friendly error page to the user when something goes wrong. Let’s see the same in action. In the below snippet, I have commented out the [Authorize] attribute, and purposely thrown one exception at the beginning of the controller itself and the result as excepted, generic error handler page of .net as shown below.

1st 2nd


so, now how to fix this kind of handled situation. The very simple change which i could do here is set the custom errors attribute’smode to On. and then, just refresh the page, then we’ll see a friendly error page as produced below.

3rd 4th


But, the reason we end up here is primarily because of HandleError attribute. it is providing post processing logic on action and it actually decides if an exception is escaped from an action, it will display an error view instead of yellow screen. the error view basically resides in the application itself, so if you go to views –> shared –> Error.cshtml page. so, basically an error view is just a razor view.



Now, let me show that thing from where, this view is getting triggered. This is from filterconfig.cs file under App_Start folder as shown below.



Now, let me show, if i comment out the above line then what will happen.

7th 8th


so, it again landed in the .net generic error handler page. So, this was the Global Filters in a nut shell. In the next section, we’ll see more about MVC, Till then stay tuned and good bye.

Happy Coding

1,290 total views, no views today