Error Handling Method 6: Day 6 of 23

This article explains error handling and the display of a page depending on the HTTP status code.

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 let's create a controller for the error handling. Right-click on "Controllers" and select "Add" >> "Controller...".



Step 7

Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.



Step 8


Name the controller "ErrorController". The Index() action result method will be added.



Step 9

Create action method "View404" inside the error controller.



Step 10

To add a view for index, right-click on "Index" and select "Add View...".



Step 11

Name the view and select "Empty (without model)" as the template. Click on the "Add" button.



Step 12

Here we add title "Error/Index Page", so that we can easily identify that the error is generated and the index page is displayed.



Step 13

To add a view for View404, right-click on "View404" and select "Add View...".



Step 14

Name the view and select "Empty (without model)" as the template. Click on the "Add" button.



Step 15

Add a title "Error/View404 Page" or design in such a way that it can be identified that it is View404 page.



Step 16

Generate an exception inside the index action method.



Step 17

Open the web.config file, under system.web.

Set customErrors mode=”On” to display a custom error page.

Set defaultRedirect=”/Error/Index”, that sets the default redirection path, when an error occurs it redirects to this path, in other words the index action method of the error controller.

Inside the customErrors we can set the redirect path for the HTTP status code. Here in our example we set the redirect path for HTTP status code 404, that means that whenever we get a response as HTTP status code 404, it is redirected to set the path, in other words the View404 action method of the error controller.



Step 18

Now run the project and you will get a NullReferenceException since session "temp" is not available.



Step 19

Press "F5" and it displays an error page.



Step 20

To generate a 404 error, first comment out the code that generates the exception in the application and run the application.



Step 21

Now modify the path in the browser, set the action method name that is not available in our application so that it can return HTTP status code 404 – file not found. Here we set Index1 that is not available in the application and press Enter.



Step 22

It redirected to the page designed for HTTP status code 404. In the same way you can design for other HTTP status codes.



Similar Articles