HandleErrorAttribute is used to display friendly error pages to end user when there is an unhandled exception.
Lets we are trying to access a view which is not part of Controller Class. At this time we will get an error page which is so technical which is of no use to end user. And sometimes it will open the door for the hacker to hack our application.
It is always a good practice to handle such type of errors and redirects to the error handler page. We need to design our application accordingly so that error page with precious information should not be viewed to the end users. Follow below steps:
Enable custom errors in the web.config file, that is present in the root directory of your MVC application. "customErrors" element must be nested under "<system.web>".
<customErrors mode="On">
</customErrors>
Add "Shared" folder under "Views" folder. Add Error.cshtml view inside this folder. Paste the following HTML in Error.cdhtml view.
<h2>An unknown problem has occured, please contact Admin</h2>
Now run the application. Whenever any error occurs it will be redirected to Error.cshtml page.
No need to apply HandleError attribute anywhere in the application as it is part of GlobalFilters
Global.asax
Lets we are trying to access a view which is not part of Controller Class. At this time we will get an error page which is so technical which is of no use to end user. And sometimes it will open the door for the hacker to hack our application.
It is always a good practice to handle such type of errors and redirects to the error handler page. We need to design our application accordingly so that error page with precious information should not be viewed to the end users. Follow below steps:
Enable custom errors in the web.config file, that is present in the root directory of your MVC application. "customErrors" element must be nested under "<system.web>".
<customErrors mode="On">
</customErrors>
Add "Shared" folder under "Views" folder. Add Error.cshtml view inside this folder. Paste the following HTML in Error.cdhtml view.
<h2>An unknown problem has occured, please contact Admin</h2>
Now run the application. Whenever any error occurs it will be redirected to Error.cshtml page.
No need to apply HandleError attribute anywhere in the application as it is part of GlobalFilters
Global.asax
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } }
HandleErrorAttribute is added to the GlobalFilters collection in global.asax. When a filter is added to the GlobalFilters collection, then it is applicable to all controllers and their action methods in the entire application.
Note that HTTP status code 404 cannot be handled by HandleError attribute. But we can handle by adding PageNotFound controller and its action methods. Keep the respective view in the shared folder to be available for the entire application.
No comments:
Post a Comment