![](/img/trans.png)
[英]How return View with model in the ExceptionFilter in Asp.Net Core MVC
[英]How to return same view from custom ExceptionFilter in mvc 5
我正在使用自定义异常文件管理器来在一个地方处理异常。 根据我的应用程序需求,同一视图将在视图顶部显示错误(业务/通用)消息,但是当我使用以下代码显示异常时,它显示空白页面,错误后不返回视图。 在这里,我不了解如何使用当前绑定的模型返回相同的视图。
这是我的ExceptionFilter
类
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>
这是管制员
public class UserController : BaseController
{
public ActionResult User()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult User(User model)
{
//Doing some db call that may throw error
throw new Exception("test exception");
return View(model);
}
}
您必须将异常过滤器属性添加到控制器方法。
[AutoExceptionHandler]
public ActionResult User(User model)
{
//Doing some db call that may throw error
throw new Exception("test exception");
return View(model);
}
看看此页面如何创建和使用过滤器属性: https : //www.c-sharpcorner.com/article/create-user-defined-filters-in-asp-net-mvc-5-in-step-分步处理/
还要确保将数据传递到请求中的视图,否则,在某些设置下(没有异常返回到用户视图且未设置错误页面),您可能会得到空白页面。
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.