C# 如何在MVC5中从自定义ExceptionFilter返回相同的视图
我使用自定义异常文件管理器在一个位置处理异常。根据我的应用程序要求,相同的视图将在视图顶部显示错误(业务/通用)消息,但当我使用下面的代码显示异常时,它将显示空白页,错误后不会返回视图。在这里,我不知道如何使用当前绑定的模型返回相同的视图 这是我的C# 如何在MVC5中从自定义ExceptionFilter返回相同的视图,c#,.net,asp.net-mvc-5,exceptionfilterattribute,C#,.net,Asp.net Mvc 5,Exceptionfilterattribute,我使用自定义异常文件管理器在一个位置处理异常。根据我的应用程序要求,相同的视图将在视图顶部显示错误(业务/通用)消息,但当我使用下面的代码显示异常时,它将显示空白页,错误后不会返回视图。在这里,我不知道如何使用当前绑定的模型返回相同的视图 这是我的例外过滤器类 public class AutoExceptionHandler : ActionFilterAttribute, IExceptionFilter { public void OnException(ExceptionCont
例外过滤器
类
public class AutoExceptionHandler : ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
Exception e = filterContext.Exception;
ModelStateDictionary modelState = ((Controller)filterContext.Controller).ModelState;
filterContext.ExceptionHandled = true;
Handle(e, modelState);
}
public void Handle(Exception ex, ModelStateDictionary modelState)
{
string message = "";
Int32? auditLogID;
Type typ = ex.GetType();
if (typ == typeof(IE.Factory.Http.HttpResponseException))
{
message = ex.Message;
}
else
{
message = MessageChannel.Instance.GetMessageDescription("GENERIC_ERR_MSG");
}
//auditLogID = Logger.SaveException(ex);
if (modelState != null)
{
modelState.AddModelError("", message);
}
}
}
这是我的观点。
@model MyApp.Model.User
@{
ViewBag.Title = "User";
Layout = "~/Views/Shared/_LayoutEmpty.cshtml";
}
<div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="msg">
<div class="@(Html.ViewData.ModelState.IsValid ? "validation-summary-valid" : "validation-summary-errors") msg-pnl"
data-valmsg-summary="true">
<div class="msg-body">
<div class="text-danger">
The following error(s) occurred:
<ul class="validation-error">
@foreach (var modelError in Model.SelectMany(keyValuePair => keyValuePair.Value.Errors))
{
<li>@modelError.ErrorMessage</li>
}
</ul>
</div>
</div>
</div>
</div>
<div>
<div>
@Html.LabelFor(model => model.Name, "User Name:", new { @class = "m-0 p-0" })
</div>
<div>
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control-required" } })
</div>
<div>
@Html.ValidationMessageFor(model => model.Name, "*", new { @class = "text-danger" })
</div>
</div>
<div>
<div>
@Html.LabelFor(model => model.Address, "Address :", new { @class = "m-0 p-0" })
</div>
<div>
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control-required" } })
</div>
<div>
@Html.ValidationMessageFor(model => model.Address, "*", new { @class = "text-danger" })
</div>
</div>
<div>
<div >
<input type="submit" value="Save" class="btn-submit" id="btnSave" />
</div>
</div>
}
</div>
您必须将异常过滤器属性添加到控制器方法中
[AutoExceptionHandler]
public ActionResult User(User model)
{
//Doing some db call that may throw error
throw new Exception("test exception");
return View(model);
}
查看此页面如何创建和使用过滤器属性:
还要确保在请求中向视图传递数据,否则在某些设置下(没有向用户视图返回异常,并且没有设置错误页面),您可能会得到一个空白页面
我希望这会有所帮助。如果您希望后端代码出现任何错误,则应仅在控制器代码中处理这些错误,并将正确的视图或数据返回给客户端或视图。ExceptionFilter用于未处理的异常。处理模型状态错误肯定不应该是异常过滤器的一部分。@ChetanRanpariya,如果我在一个地方而不是在每个控制器上处理每个错误,会造成任何影响吗?我将错误添加到模型状态只是为了测试,我计划在tempdata变量中设置错误,该变量将保存来自控制器的每个引发的错误并显示在UI上。
[AutoExceptionHandler]
public ActionResult User(User model)
{
//Doing some db call that may throw error
throw new Exception("test exception");
return View(model);
}