![](/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.