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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Error.Controllers {
public class HomeController: Controller {
//
// GET: /Home/
[HandleError]
public ActionResult Index() {
return View();
}
public ActionResult Index1() {
return View();
}
}
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Error.Controllers {
public class ErrorController: Controller {
//
// GET: /Error/
public ActionResult Index() {
return View();
}
public ActionResult NotFound() {
return View();
}
}
}
[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
@{
ViewBag.Title = "My Error Page";
}
<div style=" padding:20px; color:red" >
<h3>
My Own Created Error Page:
</h3>
<h4>
Sorry, The Page, You are Looking for File is not found Or Controller Name / Controller Action Method Name Not There.
</h4>
<h6>@Html.ActionLink("Go Back To Home Page", "Index", "Home")</h6> @*link name , action method name , controller name*@
<br />
<br />
</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
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
<table border="1">
<tr>
<td>
Name
</td>
</tr>
<tr>
<td>
Address
</td>
</tr>
<tr>
<td>
Email
</td>
</tr>
<tr>
<td>
Dob
</td>
</tr>
</table>
</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.












Join the conversation! Your thoughts help the community grow.