首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将ViewData传递给HandleError视图?

如何将ViewData传递给HandleError视图?
EN

Stack Overflow用户
提问于 2009-11-25 13:53:23
回答 2查看 8.2K关注 0票数 14

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

代码语言:javascript
复制
[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)。下面是一个简单的场景来说明我的问题:

代码语言:javascript
复制
public class TestingController : SiteController
{
  public ActionResult DoSomething()
  {
    throw new MyException("pwnd!");
  }
}

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

在这种情况下,向我的Site.Master传递ViewData参数的最佳方式是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-11-25 14:13:41

这是因为当出现错误时,HandleErrorAttribute会更改传递给视图的ViewData。它传递一个HandleErrorInfo类的实例,其中包含有关异常、控制器和操作的信息。

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

代码语言:javascript
复制
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;
            }
        }
    }
}
票数 21
EN

Stack Overflow用户

发布于 2013-04-09 07:29:37

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

代码语言:javascript
复制
    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?

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1794936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档