I am working on exception handling and have written below code for the same.
Web.Config
<customErrors mode="On"></customErrors>
Code Changes
public class CustomExceptionFilter : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
//_logger.Error("Uncaught exception", filterContext.Exception);
ViewResult view = new ViewResult();
view.ViewName = "Error";
filterContext.Result = view;
// Prepare the response code.
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// To handle exceptions
GlobalFilters.Filters.Add(new CustomExceptionFilter());
var unityContainer = ModelContainer.Instance;
DependencyResolver.SetResolver(new UnityDependencyResolver(unityContainer));
}
Error View
@*@model System.Web.Mvc.HandleErrorAttribute*@
<div class="row">
<div class="col-sm-12">
<h1>Error</h1>
<div class="col-sm-12">
<h2>An error occurred while processing your request.</h2>
</div>
</div>
</div>
In my error view, I want to show the exception message.
Also I have one more requirement to show different message in case of 404 error
exists.
Views/Shared/Error.cshtml
Web.Config
I know this is an old post but I just want to share the easiest way to do it. Just paste this in your Web.config
You can try sending like this