Part 72 HandleError attribute in mvc

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
is part 72 of asp.net mvc tutorial in this video we'll discuss handle error attribute in mvc handle error attribute is used to display friendly error pages to users when there is an unhandled exception in your application let's understand this with an example let's flip to visual studio here i have a blank asp.net mvc4 application let's add a controller let's name it home controller and let's change the implementation of the index action method to through an exception so we want to throw a new exception something went wrong is going to be the exception message so here the index action method within this home controller is throwing this exception and this exception is unhandled so let's build the solution and let's navigate to this index action within the home controller so localhost nvc demo is the project name home is the controller and index is the action method let's see what's going to happen when there is an unhandled exception look at this we will arrive at this yellow screen and this screen is referred to as yellow screen of death displaying the screen to users is bad for two reasons number one this screen is too technical it doesn't make any sense to the end user and number two notice that this screen is showing your application code this information could be useful for a hacker to hack into your application so that's why we should never be displaying this yellow screen of death instead we should be showing a friendly error page let's see how to achieve that and there are two very simple steps in mvc the first step is to turn on custom errors in your web.config file within an mvc project by default you're going to have two web.config files one web.config file is present within the root directory and the other one is present within the fuse folder so make sure you add the custom errors element to the web.config file that's present within the root directory of your project so within web.config under system.fab we want to add custom errors and we want to set the mode to on okay now if you notice the mode attribute of this custom errors element it has got three settings on off remote only now we have discussed the significance of those settings in detail in part 71 of the asp.net video tutorial all right so the second step is to add error view to the shared folder so to the views folder let's add shade folder and then to this let's add a view with name error click add so that should add the error view maybe we want to display a friendly error message to the user something unknown has happened please contact admin a friendly message to the user so let's build the solution let's come and refresh this let's see what's going to happen look at that instead of that yellow screen of death we are at this friendly error page okay now the important question to ask is we did not apply this handle error attribute anywhere so how did all this work now it turns out within web.config file there is a call to this function filter config dot register global filters so if i right click on that and then if we go to its definition so look at this within this function so we have this global filter collection object that's being passed into this and to that global filter collection we are adding this new handle error attribute so this handle error attribute is being applied globally which means it's applicable for all the controllers and for all the action methods within those controllers so we don't have to apply this attribute individually or within each controller and to each action method it's applied at the global level so it's applicable for all the controllers and for all the action methods within those controllers in the entire application so what's going to happen if i remove this or if i comment this line let's see that so i commented that let's build the solution and let's refresh this view and see what's going to happen as you might expect we are back at this yellow screen of that okay so this handle error attribute you know by default it's applied at the global level but if you prefer you can apply it at a controller level or at a specific action method now let's say if i apply it at this action method so handle error so we applied it on this action method so if this method throws an exception then that will be handled so when we refresh this it should display the friendly error page but then if i have another action method now let's call it maybe index one and if it throws an exception let's build the solution now we don't have handle error attribute on this function and it's not applied at the global level so what's going to happen we're going to be back at that yellow screen of death let's build the solution and actually let's navigate to index one action method look at that we are back at that screen that's why it makes more sense to apply that handle error attribute globally all right so let's undo these changes that we have done let's remove that handle error attribute from the action method let's go back to that filter config and uncomment that line so that we have the filter applied at the global level all right now let's try to navigate to the index action method and see if we get a friendly error page yes we do now is the friendly error page displayed for http status code 404 now what is this http status code 404 now look at this within the home controller at the moment we have index action and index 1x action so if i navigate to index or index 1 then you know since these action methods are throwing exceptions we have a custom error page you know that custom error page is displayed but let's say i am navigating to something called list within the home controller but within the home controller do we have a list action method no we don't so what's going to happen when i try to visit that will i get the friendly error page no okay so how do we handle this http status code 404 now again i don't want end users to be looking at the screen instead i want to show him another friendly error page which says that you know the page that you're looking for cannot be found please check your url you know a friendly message to the user so how to achieve that again there are three simple steps the first step is to add a controller and you can name this controller anything but since we are using this controller to handle errors i'm going to give it a meaningful name error controller so let's go ahead and add that so right click on the controllers folder add a controller and let's call it error controller and again within this controller you can have the action method any name but then we are going to deal with not found exception so i'm going to name it not found okay and then obviously this action method is going to return a view so we need to add a view but i'm going to add that view to the shader folder and let's name it not found let's say the page you are looking for cannot be found please check the url a meaningful message to the user all right and the final step is to tweak this custom errors element okay now what is the status quo that we are getting http status code 404 that's the error code so within web.config file this is what we need to do we need to say okay add an error element and then set the status code so for this status code 404 invoke not found action method within the error controller so within the error controller we have not found action method which is returning a view and what view does it return it returns a view with the same name and we do have a view with that name within the shared folder so this view will be returned if there is going to be an http status code 404 so let's make those changes within our web.config file so error element the status code is going to be 404 and we want to redirect the user to error controller and what is the action method name not found action let's build the solution and now let's try to refresh this so list is not found within the home controller let's refresh this and see if we get the custom error page that we have defined look at that the page you're looking for cannot be found please check the url all right so three simple steps to handle those http status code 404 errors that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 158,486
Rating: undefined out of 5
Keywords: handleerror, attribute, action filter, mvc, asp.net, HandleErrorAttribute, custom errors
Id: nNEjXCSnw6w
Channel Id: undefined
Length: 10min 10sec (610 seconds)
Published: Wed Aug 07 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.