我正在研究异常处理,并为此编写了下面的代码。
网页配置
<customErrors mode="On"></customErrors>
代码变更
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;
}
}
全局.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));
}
错误查看
@*@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>
在我的错误视图中,我想显示异常消息。
另外,我还有一个要求,要在以下情况下显示不同的消息:404 error
exists.
public class CustomExceptionFilter : HandleErrorAttribute {
public override void OnException(ExceptionContext filterContext) {
var controller = filterContext.Controller as Controller;
controller.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
controller.Response.TrySkipIisCustomErrors = true;
filterContext.ExceptionHandled = true;
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var exception = filterContext.Exception;
//need a model to pass exception data to error view
var model = new HandleErrorInfo(exception, controllerName, actionName);
var view = new ViewResult();
view.ViewName = "Error";
view.ViewData = new ViewDataDictionary();
view.ViewData.Model = model;
//copy any view data from original control over to error view
//so they can be accessible.
var viewData = controller.ViewData;
if (viewData != null && viewData.Count > 0) {
viewData.ToList().ForEach(view.ViewData.Add);
}
//Instead of this
////filterContext.Result = view;
//Allow the error view to display on the same URL the error occurred
view.ExecuteResult(filterContext);
//should do any logging after view has already been rendered for improved performance.
//_logger.Error("Uncaught exception", exception);
}
}
视图/共享/Error.cshtml
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<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>
@{
if (Model != null && Model.Exception != null) {
<h5>Here is some information you can pass on to the administrator</h5>
<h3>Path: ~/@string.Format("{0}/{1}", Model.ControllerName, Model.ActionName)</h3>
<h4>Error: @Model.Exception.GetType().Name</h4>
if (Request.IsLocal) {
<h4>Message: @Model.Exception.Message</h4>
<h5>@Model.Exception.StackTrace</h5>
}
}
}
</div>
</div>
</div>
网页配置
<customErrors mode="Off"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)