This article explains error handling using global filters for all controllers and how to display a custom view.
Step 1
Create a MVC project from the "Empty" template. Right-click on "Controllers" and select "Add" >> "Controller...".
Step 2
Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.
Step 3
Name the controller "HomeController". The Index() action result method will be added.
Step 4
To add a view, right-click on "Index" and select "Add View...".
Step 5
Name the view and select "Empty (without model)" as the template. Click on the "Add" button.
Step 6
Now we need to create a custom error page so that when an error occurrs, this custom error page is displayed. This error page is common for many kinds of errors generated by any page. So we create it under the shared folder.
Right-click on “Views”, select “Add” >> “New Folder”.
Step 7
Name the folder "Shared"; this will create a "Shared" folder under the view.
Step 8
Right-click on the "Shared" folder and select "Add" >> "View…".
Step 9
Name the view as "Error" and select "Empty (without model)" as the template.
Step 10
Design your desired custom error page in Error.cshtml.
Step 11
Open the web.config file, set customErrors mode=”On” under system.web. If you do not want to display a custom error page then you can set it to “Off”.
Step 12
Let's create a page that we want to display (not an error page). in other words help page. This page we should create under the “Shared” folder.
Right-click on the “Shared” folder, select “Add” >> “View…”.
Step 13
Name the view as "Help" and select "Empty (without model)" as the template.
Step 14
Design your desired help page in Help.cshtml.
Step 15
Generate an exception inside the index action method.
Step 16
Right-click on “App_Start”, select “Add” >> “Class…”.
Step 17
Select “Class” from the installed template and provide an appropriate name like "ErrorConfig".
Step 18
In the following code snippet you can see the static method that accepts a GlobalFilterCollection. Create an object of HandleErrorAttribute and set the view that you want to display when an exception is thrown. Then add a HandleErrorAttribute object to the GlobalFilterCollection.
Step 19
Now call this static method inside the Application_Start event of Global.asax.cs, so that it will implement as soon as the application starts.
Step 20
Now run the project and you will get a NullReferenceException since the session "temp" is not available.
Step 21
Press "F5" and it displays a help page, not an error page.
Step 1
Create a MVC project from the "Empty" template. Right-click on "Controllers" and select "Add" >> "Controller...".
Step 2
Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.
Step 3
Name the controller "HomeController". The Index() action result method will be added.
Step 4
To add a view, right-click on "Index" and select "Add View...".
Step 5
Name the view and select "Empty (without model)" as the template. Click on the "Add" button.
Step 6
Now we need to create a custom error page so that when an error occurrs, this custom error page is displayed. This error page is common for many kinds of errors generated by any page. So we create it under the shared folder.
Right-click on “Views”, select “Add” >> “New Folder”.
Step 7
Name the folder "Shared"; this will create a "Shared" folder under the view.
Step 8
Right-click on the "Shared" folder and select "Add" >> "View…".
Step 9
Name the view as "Error" and select "Empty (without model)" as the template.
Step 10
Design your desired custom error page in Error.cshtml.
Step 11
Open the web.config file, set customErrors mode=”On” under system.web. If you do not want to display a custom error page then you can set it to “Off”.
Step 12
Let's create a page that we want to display (not an error page). in other words help page. This page we should create under the “Shared” folder.
Right-click on the “Shared” folder, select “Add” >> “View…”.
Step 13
Name the view as "Help" and select "Empty (without model)" as the template.
Step 14
Design your desired help page in Help.cshtml.
Step 15
Generate an exception inside the index action method.
Step 16
Right-click on “App_Start”, select “Add” >> “Class…”.
Step 17
Select “Class” from the installed template and provide an appropriate name like "ErrorConfig".
Step 18
In the following code snippet you can see the static method that accepts a GlobalFilterCollection. Create an object of HandleErrorAttribute and set the view that you want to display when an exception is thrown. Then add a HandleErrorAttribute object to the GlobalFilterCollection.
Step 19
Now call this static method inside the Application_Start event of Global.asax.cs, so that it will implement as soon as the application starts.
Step 20
Now run the project and you will get a NullReferenceException since the session "temp" is not available.
Step 21
Press "F5" and it displays a help page, not an error page.
Source collected from csharpcorner.com
http://www.c-sharpcorner.com/UploadFile/db2972/error-handling-method-4-day-4-of-23/
No comments :
Post a Comment