簡體   English   中英

ASP.MVC中的HandleError和強類型布局

[英]HandleError and strongly typed layout in ASP.MVC

我正在使用ASP.MVC4。我有一個強類型的布局,該布局已連接到基本視圖模型(每個其他視圖模型都從基本視圖模型繼承)。 我正在嘗試使用標准的HandleError過濾器來處理錯誤。 它是開箱即用的配置,如果布局不是強類型的,則可以使用。

我的示例異常如下:

public class TestController : Controller
{
    public ActionResult Index()
    {
        throw new Exception("oops");
    }
}

當該機制嘗試將“錯誤”頁面插入強類型布局時,就會遇到麻煩,因為它沒有模型。

有誰知道在HandleError過濾器中使用強類型布局的情況? 是否可以在引發異常之前為布局設置模型?

編輯:

可能的解決方案是關閉標准錯誤處理機制,並在Global.asax的Application_Error方法中捕獲異常。 您可以在此處找到示例解決方案: ASP.MVC HandleError屬性不起作用

無論如何-如果有人有其他解決方案,請發布。

您可以編寫自己的HandleError屬性,然后將其注冊到FilterConfig而不是默認屬性。 您的自定義變量將擴展默認屬性,並覆蓋結果,因此傳遞到視圖中的模型實例為必需的類型:

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {
        //When already handled, do nothing
        if (context.ExceptionHandled)
            return;

        //Run the base functionality
        base.OnException(context);

        //If the base functionality didnt handle the exception, then exit (as the exception is not of the type this filter should handle)
        if (!context.ExceptionHandled) return;

        //Set a view as the result           
        context.Result = GetViewResult(context);            
    }

    private ViewResult GetViewResult(ExceptionContext context)
    {
        //The model of the error view (YourModelType) will inherit from the base view model required in the layout
        YourModelType model = new YourModelType(context.Exception, ...);
        var result = new ViewResult
        {
            ViewName = View,
            ViewData = new ViewDataDictionary<YourModelType>(model),
            TempData = context.Controller.TempData
        };
        return result;
    }
}

然后在全局范圍內注冊此過濾器,而不是默認的HandleErrorAttribute

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new CustomHandleErrorAttribute());
    ...
}

我唯一能想到的是

  • a)在Global.asax.cs中,處理Application_Error(或Application_OnError,簽名現在轉義了我)並重定向到強類型視圖
  • b)編輯web.config customErrors部分以指向您的強類型視圖。 但是,執行b)可以像其他方法一樣容易,例如benfoster.io/blog/aspnet-mvc-custom-error-pages

您可以通過執行以下操作來獲取重現該錯誤的控制器名稱和操作名稱:

public class CustomErrorException: HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        filterContext.Result = new ViewResult {
            ViewName = "CustomError",
            ViewData = new ViewDataDictionary<HandleErrorInfo> { 
                Model = new HandleErrorInfo(
                    exception: filterContext.Exception,
                    controllerName: filterContext.RequestContext.RouteData.Values["controller"].ToString(),
                    actionName: filterContext.RequestContext.RouteData.Values["action"].ToString()
                ) 
            }
        };

        //You can log your errors here.
        filterContext.ExceptionHandled = true;
    }
}

在“ CustomError.cshtml”視圖中,您可以執行以下操作:
@model HandleErrorInfo
嘿,這是一個自定義錯誤!!!
控制器名稱:@ Model.ControllerName
動作名稱:@ Model.ActionName
異常消息:@ Model.Exception.Message

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM