如何将ViewData传递给HandleError视图?

时间:2009-11-25 05:53:23

标签: asp.net-mvc viewdata handleerror

在我的Site.Master文件中,我有3个简单的ViewData参数(在我的整个解决方案中只有3个)。这些ViewData值对于我的应用程序中的每个页面都至关重要。由于这些值在我的Site.Master中使用,我创建了一个抽象的SiteController类,它覆盖了OnActionExecuting方法,以便为我的解决方案中的每个控制器上的每个Action方法填充这些值。

[HandleError(ExceptionType=typeof(MyException), View="MyErrorView")]
public abstract class SiteController : Controller
{
  protected override void OnActionExecuting(...)
  {
    ViewData["Theme"] = "BlueTheme";
    ViewData["SiteName"] = "Company XYZ Web Portal";
    ViewData["HeaderMessage"] = "Some message...";        

    base.OnActionExecuting(filterContext);

  }
}

我遇到的问题是,当HandleErrorAttribute从SiteController类级别属性启动时,这些值不会传递给MyErrorView(最终是Site.Master)。这是一个显示我的问题的简单方案:

public class TestingController : SiteController
{
  public ActionResult DoSomething()
  {
    throw new MyException("pwnd!");
  }
}

我已经尝试通过覆盖SiteController中的OnException()方法来填充ViewData参数,但无济于事。 :(

在这种情况下,将ViewData参数传递给Site.Master的最佳方法是什么?

2 个答案:

答案 0 :(得分:20)

这是因为HandleErrorAttribute会在发生错误时更改传递给视图的ViewData。它传递一个HandleErrorInfo类的实例,其中包含有关Exception,Controller和Action的信息。

您可以做的是将此属性替换为下面实现的属性:

using System;
using System.Web;
using System.Web.Mvc;

public class MyHandleErrorAttribute : HandleErrorAttribute {

    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }
        if (!filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled)
        {
            Exception innerException = filterContext.Exception;
            if ((new HttpException(null, innerException).GetHttpCode() == 500) && this.ExceptionType.IsInstanceOfType(innerException))
            {
                string controllerName = (string) filterContext.RouteData.Values["controller"];
                string actionName = (string) filterContext.RouteData.Values["action"];
                // Preserve old ViewData here
                var viewData = new ViewDataDictionary<HandleErrorInfo>(filterContext.Controller.ViewData); 
                // Set the Exception information model here
                viewData.Model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
                filterContext.Result = new ViewResult { ViewName = this.View, MasterName = this.Master, ViewData = viewData, TempData = filterContext.Controller.TempData };
                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.StatusCode = 500;
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            }
        }
    }
}

答案 1 :(得分:3)

如果你只需要通过ViewBag传递一些东西,就可以这样做:(这恰好在我所有控制器继承的BaseController中)

    protected override void OnException(ExceptionContext filterContext)
    {
        try
        {
            var v = filterContext.Result as ViewResult;
            v.ViewBag.DataToPassToError = "Hello World!";
        }
        catch { /* no-op */ }

        base.OnException(filterContext);
    }

见这里:How can I use data placed into a ViewBag by a filter in my Error.cshtml view?

相关问题