Please follow the steps.this is the custom error handling which helps you display the exception in your error page
Create a Controller and named it as Error controller and the actionresults like below
public class ErrorController : Controller
{
public ActionResult Index()
{
var error = ViewData.Model;
return View(error);
}
}
Create view by right clicking the index view
@model HandleErrorInfo
@{
ViewBag.Title = "Server Error";
}
<h2 class="errorh">Server Error</h2>
<div>Controller: @Model.ControllerName</div>
<div>Action Name : @Model.ActionName</div>
<div>Exception: @Model.Exception.Message</div>
<div id="moreDetails">
<div>Stack Trace: @Model.Exception.StackTrace</div>
<div>Inner Exception: @Model.Exception.InnerException</div>
</div>
put the below in below in application_error method in the Global.asax file
protected void Application_Error(object sender, EventArgs e)
{
var httpContext = ((MvcApplication)sender).Context;
var currentController = " ";
var currentAction = " ";
var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
if (currentRouteData != null)
{
if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
{
currentController = currentRouteData.Values["controller"].ToString();
}
if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
{
currentAction = currentRouteData.Values["action"].ToString();
}
}
var ex = Server.GetLastError();
var controller = new ErrorController();
var routeData = new RouteData();
var action = "Index";
if (ex is HttpException)
{
var httpEx = ex as HttpException;
switch (httpEx.GetHttpCode())
{
case 404:
action = "NotFound";
break;
case 401:
action = "AccessDenied";
break;
}
}
httpContext.ClearError();
httpContext.Response.Clear();
httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
httpContext.Response.TrySkipIisCustomErrors = true;
routeData.Values["controller"] = "Error";
routeData.Values["action"] = action;
controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
}
Hope this helps