mError handling and override OnException method and display custom 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 6Generate an exception inside the index action method.
Step 7In order to create a custom error page, right-click on “Views”, select “Add” >> “New Folder”.
Step 8Name the folder "Shared", this will create a "Shared" folder under the view.
Step 9Right-click on the "Shared" folder and select "Add" >> "View…".
Step 10Name the view "Error" and select "Empty (without model)" as the template.
Step 11Inside error.cshtml, add a meaningful title to this page.
Step 12Now override the OnException method. First check the value of ExceptionHandled, if it is true then just return, else create an object of viewResult and set the ViewName, in other words a custom error page, that we want to display when an exception occurs. Finally set the value of ExceptionHandled to true.
Step 13Run the project in debug mode and it will throw an exception inside the Index action method. Now continue with debugging and it will jump to the OnException method.
Step 14Here you can see that it will jump to the OnException method.
Step 15Now press F5 and you can see that custom error page is rendered in the browser.
Step 16Now a question may arise in your mind that what if we do not set ExceptionHandled to true. So if you do not set ExceptionHandled to true, then its value remains false and it will jump to the Application_Error method inside Global.asax.cs. Let's get the exception details inside Application_Error using Server.GetLastError().
Step 17To verify the step 16 comment the line where we set the value of ExceptionHandled to true inside the OnException method.
Step 18Run the project in debug mode and the exception is thrown inside the Index action method.
Step 19Continue debugging and it will jump to the OnException method. Here you can check the value of ExceptionHandled and it is false.
Step 20Continue debugging and you will jump to Application_Error and here you get the exception details.
Step 21Now press F5 and a server error page is displayed. So if we do not set ExceptionHandled to true then the custom error page will not be rendered and it renders the 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 6Generate an exception inside the index action method.
Step 7In order to create a custom error page, right-click on “Views”, select “Add” >> “New Folder”.
Step 8Name the folder "Shared", this will create a "Shared" folder under the view.
Step 9Right-click on the "Shared" folder and select "Add" >> "View…".
Step 10Name the view "Error" and select "Empty (without model)" as the template.
Step 11Inside error.cshtml, add a meaningful title to this page.
Step 12Now override the OnException method. First check the value of ExceptionHandled, if it is true then just return, else create an object of viewResult and set the ViewName, in other words a custom error page, that we want to display when an exception occurs. Finally set the value of ExceptionHandled to true.
Step 13Run the project in debug mode and it will throw an exception inside the Index action method. Now continue with debugging and it will jump to the OnException method.
Step 14Here you can see that it will jump to the OnException method.
Step 15Now press F5 and you can see that custom error page is rendered in the browser.
Step 16Now a question may arise in your mind that what if we do not set ExceptionHandled to true. So if you do not set ExceptionHandled to true, then its value remains false and it will jump to the Application_Error method inside Global.asax.cs. Let's get the exception details inside Application_Error using Server.GetLastError().
Step 17To verify the step 16 comment the line where we set the value of ExceptionHandled to true inside the OnException method.
Step 18Run the project in debug mode and the exception is thrown inside the Index action method.
Step 19Continue debugging and it will jump to the OnException method. Here you can check the value of ExceptionHandled and it is false.
Step 20Continue debugging and you will jump to Application_Error and here you get the exception details.
Step 21Now press F5 and a server error page is displayed. So if we do not set ExceptionHandled to true then the custom error page will not be rendered and it renders the error page.
Source collected from CSharpcorner.com
http://www.c-sharpcorner.com/UploadFile/db2972/error-handling-method-7-day-7-of-23/
No comments :
Post a Comment