1
我正在使用ASP.NET Core。我的一個控制器調用引發各種異常的服務。我想在一個異常過濾器(不是中間件)中處理它們。從異常過濾器重定向
public class MyHandlerAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext c)
{
if (c.Exception is FooException) {
// redirect with arguments to here
}
else if (c.Exception is FooException) {
// redirect with arguments to there
}
else {
// redirect to main error action without arguments, as 500
}
base.OnException(c);
}
}
與行動過濾器,一個異常過濾器不給我訪問Controller
,所以我不能做一個c.Result = controller.RedirectTo...()
。
那麼,如何重定向到我的錯誤操作?
第二個不起作用。第一個工作,如果你也設置'context.Exception = null;'但是這種類型的重定向不允許我指定參數 - 這是可能的嗎? – grokky
第二個選項也適用。但是你也應該設置'context.ExceptionHandled = true;'。 – juunas
@juunas如果我這樣做,那麼它只是呈現一個空白頁...奇怪! – grokky