Error Page Customization Setting In ASP.NET MVC 4.0

Note

We can manually customize an error page In ASP.NET and ASP.NET MVC 4.0.

Step 1

First, create an Application named “Error”.


Step 2

Now, create a controller class file named “HomeController.cs”.

Code Ref 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace Error.Controllers {  
  7.     public class HomeController: Controller {  
  8.         //  
  9.         // GET: /Home/  
  10.         [HandleError]  
  11.         public ActionResult Index() {  
  12.             return View();  
  13.         }  
  14.         public ActionResult Index1() {  
  15.             return View();  
  16.         }  
  17.     }  
  18. }   

HandleError provides built-in exception filters. The HandleError attribute in ASP.NET MVC can be applied over the action method as well as controller or at the global level.

Home/Index or Home/Index1, it will show error.cshtml as view not found in index1 but in web.config file, the error statusCode="404" means the file is not found and it means the controller name or controller action method name.

[HandleError] //If I put this attribute and run, it will go to Customized Bydefault error page.


Step 3

Now, create a controller class file named “ErrorController.cs”.

Code Ref 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace Error.Controllers {  
  7.     public class ErrorController: Controller {  
  8.         //  
  9.         // GET: /Error/  
  10.         public ActionResult Index() {  
  11.             return View();  
  12.         }  
  13.         public ActionResult NotFound() {  
  14.             return View();  
  15.         }  
  16.     }  
  17. }   

[HandleError] //If I put this attribute and run, using Error/Index, it will go to Customized Bydefault Error page.


Step 4

Now, go to Views folder, create a view named “NotFound.cshtml” In Views/Error folder.

Code Ref 

  1. @{  
  2.     ViewBag.Title = "My Error Page";  
  3. }  
  4.   
  5.     <div style=" padding:20px; color:red" >  
  6.         <h3>  
  7.             My Own Created Error Page:  
  8.         </h3>  
  9.         <h4>  
  10.             Sorry, The Page, You are Looking for File is not found Or Controller Name / Controller Action Method Name Not There.  
  11.         </h4>  
  12.         <h6>@Html.ActionLink("Go Back To Home Page""Index""Home")</h6> @*link name , action method name , controller name*@  
  13.         <br />  
  14.         <br />  
  15.     </div>   

This error file will be shown when the file is not found or Controller Name / Controller Action Method Name is not there.


Step 5

Now, go to Views folder, create a view named “Index.cshtml” In Views/Home folder.

Code Ref 

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <h2>Index</h2>  
  6.   
  7. <div>  
  8.     <table border="1">  
  9.         <tr>  
  10.             <td>  
  11.                 Name  
  12.             </td>  
  13.         </tr>  
  14.         <tr>  
  15.             <td>  
  16.                 Address  
  17.             </td>  
  18.         </tr>  
  19.         <tr>  
  20.             <td>  
  21.                 Email  
  22.             </td>  
  23.         </tr>  
  24.         <tr>  
  25.             <td>  
  26.                 Dob  
  27.             </td>  
  28.         </tr>  
  29.     </table>  
  30. </div>   

This Index file will be shown when the file is found or Controller Name/ Controller Action Method Name is there.


Step 5

Visual Studio by default is creating two Cshtml files i.e. _Layout.cshtml and Error.cshtml inside Shared folder.


  1. The _Layout.cshtml file is for creating any Master Page file in MVC.
  2. In this cshtml, put your own layout for master page purpose, as we did it in ASP. NET earlier.
  3. This _Layout.cshtml is referred in _ViewStart.cshtml.
  4. The _ViewStart.cshtml file contains the _Layout.cshtml to show the master page in every page by mentioning it in other cshtml file like Index.Cshtml file.
  5. If you want to apply Master page In MVC, only mention cshtml page reference of ViewStart.cshtml in other child cshtml file instead of _Layout.cshtml.



Step 6

Now, put your own code in Error.Cshtml file by removing the existing code One.

Existing Code Ref 

  1. @*@{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6. <html>  
  7. <head>  
  8.     <meta name="viewport" content="width=device-width" />  
  9.     <title>Error</title>  
  10. </head>  
  11. <body>  
  12.     <hgroup>  
  13.         <h1>Error.</h1>  
  14.         <h2>An error occurred while processing your request.</h2>  
  15.     </hgroup>  
  16. </body>  
  17. </html>*@   

My Own Code Ref 

  1. @model System.Web.Mvc.HandleErrorInfo  
  2. @{  
  3.     ViewBag.Title = "Customize Error Page";  
  4. }  
  5.   
  6.     <div style=" padding: 20px; color:red">  
  7.         <h3>  
  8.             Customized By Default Error Page:  
  9.         </h3>  
  10.         <h4>  
  11.             Sorry, an error occurred while processing your request Or View Of Corresponding Controller and Controller Action Method Not There.  
  12.         </h4>  
  13.         <h6>@Html.ActionLink("Go Back To Home Page""Index""Home")</h6> @*link name , action method name , controller name*@  
  14.         <br />  
  15.         <br />  
  16.     </div>  

This file will be shown while processing your request or the view of the corresponding controller and controller action method is there.

Step 7

Go to Web.config file. Add some code for file not found exception.

Code Ref 

  1. <system.web>  
  2.    
  3.     <customErrors mode="On"> <!--put your own customized error page-->  
  4.       <error statusCode="404" redirect="~/Error/NotFound"/>  
  5.     </customErrors>  
  6. </system.web>   

Similar to the code given above, you can create for other exceptions related to the page (Controllers & Controller Action Methods).

Controller name / action method name--> <!--Only when file not found, it will show as error code is 404-->

  1. <!--<error statusCode="error statusCode no" redirect="path"/>  
  2. <error statusCode="error statusCode no" redirect="path"/>  
  3. <error statusCode="error statusCode no" redirect="path"/>-->  

Step 8

Go to RouteConfig.cs file Of App_Start folder, add some code to set the start page, as page loads first time.

Code Ref

defaults: new { controller = "Home", action = "Index", id=UrlParameter.Optional }


Output

http://localhost:52218/home/index

Available Home Controller and Controller Action Method and Index View name are given.


OutPut

http://localhost:52218/home/index1

Home Controller and Controller Action Method name are available there but Index1 View name is not there.

Your request or the view of the corresponding Controller and Controller Action Method are not there.


OutPut

http://localhost:52218/Error/NotFound?aspxerrorpath=/home1/index1

Home Controller and Controller Action Method are available but View name is not there i.e. Home1 and Index1.

The page, you are Looking for the file is not found or Controller Name / Controller Action Method Name is not there.

In the URL given above, the path /Error/NotFound is configured in Web.Config file.


OutPut

How HTML Action Link Helper class acts Like ASP.NET Link button and It will redirect to the mentioned page or Controller name and Action method name.

Code Ref 

  1. @Html.ActionLink("Go Back To Home Page""Index""Home")</h6>  
  2. @*link name , action method name , controller name*@ 



Output of the redirected page is mentioned in Action Link.


Summary

Here, we knew that how to reconfigure an error page in web.config file and show the error of the file not found on an exception page to the end user.

Happy codining

Best of luck.

Ebook Download
View all
Learn
View all