Custom Error Page in ASP.NET MVC

Custom Error Page

Procedure

  • First add an Error.cshtml page (View Page) to the Shared Folder if it does not already exist.

  • Add or modify the Web.config file and set the Custom Error Element to On.

  • Add a specific Action Controller and View for showing the HTTP Status Code.

  • Add an [HandleError] attribute to the Targeted Action Method.

Note: When we are working on an internet application, by default it contains an Error.cshtml file.

Add a View Page. Right-click Solution Explorer, click View Folder, go to Shared Folder and name it Error.cshtml.

Solution Explorer

Add a View

Named as Error

Then design the Error Page depending on your requirements, if it already exists then modify it to suit your needs.

Error.cshtml

  1. @model System.Web.Mvc.HandleErrorInfo  
  2.   
  3. @{  
  4.     ViewBag.Title = "Error";  
  5. }  
  6.   
  7. <div style="background-color: #A52A2A; color: White; height: 10px;">  
  8. </div>  
  9. <div style="background-color: #F5F5DC; color: White; height: 170px;">  
  10.     <div style=" padding:20px;">  
  11.         <h3>  
  12.             Application Error:</h3>  
  13.         <h4>  
  14.             Sorry, an error occurred while processing your request.  
  15.         </h4>  
  16.         <h6>@Html.ActionLink("Go Back To Home Page""Index""Home")</h6>  
  17.         <br />  
  18.         <br />  
  19.     </div>  
  20. </div>  
  21. <div style="background-color: #A52A2A; color: White; height: 20px;">  
  22. </div>  
Go to Web.config file

 

  • There are two Web.config files in an ASP.NET MVC Project.

  • Go to Web.config file at the root directory.

    View that is available

  • Go to Root Directory, Web.config, then System.Web, and click CustomError.
    Set it to On.

    Add this line.
    1. <customErrors mode="On">  
    2. </customErrors>  
    Run the application and search for anything, or Link that is not available.

As example:

  • Try any Link or try to navigate to any View that is available.

    ActionResult

  • Then again try another link that is not available or modify the preceding link and watch the difference.

  • This will show you your Customized Error page rather than a default page.

Step: Try to run or Browse for a View Page that is not available (A Controller whose View is not added.).

But before that, add this Attribute to the Controller.

At the Controller:

  1. [HandleError]  
  2. public ActionResult Index()  
  3. {  
  4.    return View();  
  5. }  
Attributes

Repeat Step: Try to run or Browse for a View Page that is not available (A Controller whose View is not added.).

add the Attributes

 

  • If you add the Attributes [HandleError] to any Action Method, you will be shown your own Customized Error page written now, rather than the default Error Page.

Now add specific error pages based on the HTTP Status Code.

  • Such as one specific Error Page to show when the HTTP Status Code is 404.

  • Add a Controller to the Controller Folder and name it Error.

    Add a Controller

    Controller

  • Add some action method to the Controller.

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

Add a View Named NotFound to the Shared Folder like, as you have done earlier.

Note: We are adding this View to the Shared Folder, because Views inside Shared Folder is available to the complete application.

Right-click then select View, then go to Shared Folder and Add a View named NotFound.

Shared Folder

Add a View Named

And Design the View depending on your requirements.

Not Found.cshtml

  1. @{  
  2.     ViewBag.Title = "NotFound";  
  3. }  
  4.   
  5. <div style="background-color: #A52A2A; color: White; height: 10px;">  
  6. </div>  
  7. <div style="background-color: #F5F5DC; color: White; height: 170px;">  
  8.     <div style=" padding:20px;">  
  9.         <h3>  
  10.             Application Error:</h3>  
  11.         <h4>  
  12.             Sorry, The Page, You are Looking for is not found.  
  13.         </h4>  
  14.         <h6>@Html.ActionLink("Go Back To Home Page""Index""Home")</h6>  
  15.         <br />  
  16.         <br />  
  17.     </div>  
  18. </div>  
  19. <div style="background-color: #A52A2A; color: White; height: 20px;">  
  20. </div>  
Again go to the Root folder, then Web.config file. Go inside System.web and modify it.
  1. <customErrors mode="On">  
  2.    <error statusCode="404" redirect="~/Error/NotFound"/>  
  3. </customErrors>  
Explanation

Explanation

Now run the application, try to navigate to an unavailable View and you will see the Customized Error Page rather than the default Error Page.

Default Error Page: Example

Default Error Page

Customized Error Page:

Customized Error Page

 

  • Now, if any error occurs then the Customized Error Page will be shown.

This was all about how to display a custom error page in ASP.NET MVC.

Similarly, design all other Custom Error page depending on HTTP Status Code.

Note: The preceding information is gathered by me depending on the results of studying, experience, Internet Browsing, and Video Tutorials.


Similar Articles