[英]How to redirect to view from global exception filter in Asp.Net MVC
我正在使用asp.net core處理 MVC,我在其中創建了一個全局過濾器來處理異常。 它處理異常但不重定向到共享文件夾中的視圖(Error.cshtml)。
下面是我的代碼:
public void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
{
return;
}
Exception e = filterContext.Exception;
filterContext.ExceptionHandled = true;
string action = filterContext.RouteData.Values["action"].ToString();
string controller = filterContext.RouteData.Values["controller"].ToString();
filterContext.Result = new ViewResult()
{
ViewName = "/Error" // also tried with "Error" and "~/Error"
};
}
我希望這對你有用
protected override void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
//Redirect or return a view, but not both.
filterContext.Result = RedirectToAction("Index", "Home");
// OR
filterContext.Result = new ViewResult
{
ViewName = "~/Views/Shared/Error.cshtml"
};
}
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Account" },
{ "action", "Index" },
{ "returnUrl", filterContext.HttpContext.Request.RawUrl }
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.