如何传递的ViewData到的HandleError观? [英] How do I pass ViewData to a HandleError View?

查看:135
本文介绍了如何传递的ViewData到的HandleError观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Site.Master文件,我有3个简单的ViewData的参数(在我的整个解决方案的唯一3)。这些ViewData的值是在我的应用程序的每一个页面的关键。由于这些值是在我的Site.Master用,我创建了一个覆盖OnActionExecuting方法来填充这些值在我的解决方案的每个控制器上的每一个动作方法的抽象类SiteController。

In my Site.Master file, I have 3 simple ViewData parameters (the only 3 in my entire solution). These ViewData values are critical for every single page in my application. Since these values are used in my Site.Master, I created an abstract SiteController class that overrides the OnActionExecuting method to fill these values for every Action method on every controller in my solution.

[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);

  }
}

这是我面临的问题是,这些价值不会被传递到MyErrorView(最终的Site.Master)当HandleErrorAttribute从SiteController类级别的属性在踢。下面是一个简单的场景,以显示我的问题:

The problem that I'm faced with is that these values are not being passed to MyErrorView (and ultimately Site.Master) when the HandleErrorAttribute kicks in from the SiteController class level attribute. Here is a simple scenario to show my problem:

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

我试图通过覆盖在我的SiteController onException的()方法以及填充ViewData的参数,但无济于事。 (

I've tried filling the ViewData parameters by overriding the OnException() method in my SiteController as well, but to no avail. :(

这是通过在此情况下的ViewData参数到我的Site.Master的最佳方法?

What is the best way to pass the ViewData parameters to my Site.Master in this case?

推荐答案

这是因为HandleErrorAttribute改变的ViewData传递到视图时发生错误。它通过HandleErrorInfo类有关的异常,控制器和动作信息的实例。

This is because HandleErrorAttribute changes the ViewData passed to the view when an error occurs. It passes an instance of HandleErrorInfo class with information about Exception, Controller and Action.

你可以做的是更换一个低于实施该属性:

What you can do is replace this attribute with the one implemented below:

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;
            }
        }
    }
}

这篇关于如何传递的ViewData到的HandleError观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆