我如何可以使用过滤器在我Error.cshtml观点放到ViewBag数据? [英] How can I use data placed into a ViewBag by a filter in my Error.cshtml view?

查看:500
本文介绍了我如何可以使用过滤器在我Error.cshtml观点放到ViewBag数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动作过滤器,它负责将一些常用信息存入ViewBag用于在共享文件_Layout.cshtml所有视图。

I have an action filter that is responsible for placing some common information into the ViewBag for use by all views in the shared _Layout.cshtml file.

public class ProductInfoFilterAttribute : ActionFilterAttribute
{
    public override void
    OnActionExecuting(ActionExecutingContext filterContext)
    {
        //  build product info
        //  ... (code omitted)

        dynamic viewBag = filterContext.Controller.ViewBag;
        viewBag.ProductInfo = info;
    }
}

在共享_Layout.cshtml文件,我使用已经投入ViewBag的信息。

In the shared _Layout.cshtml file, I use the information that has been put into the ViewBag.

...
@ViewBag.ProductInfo.Name
...

如果在处理一个控制器动作发生异常,标准HandleErrorAttribute应该显示我的共享Error.cshtml视图,这个工作之前,我首先介绍了行为过滤器,并使用_Layout.cshtml从ViewBag新值开始。现在我得到的是标准的ASP.Net运行时错误页面,而不是我的自定义Error.cshtml视图。

If an exception occurs while processing a controller action, the standard HandleErrorAttribute should display my shared Error.cshtml view, and this worked before I introduced the action filter above and started using the new values from ViewBag in _Layout.cshtml. Now what I get is the standard ASP.Net runtime error page instead of my custom Error.cshtml view.

我已经跟踪下来的事实,而渲染错误观点,一个RuntimeBinderException(无法执行运行时对空引用绑定)被抛出在_Layout.cshtml使用ViewBag.ProductInfo.Name的。

I have tracked this down to the fact that while rendering the error view, a RuntimeBinderException ("Cannot perform runtime binding on a null reference") is thrown on the use of ViewBag.ProductInfo.Name in _Layout.cshtml.

这首诗我Error.cshtml视图时出现,即使我的行动过滤器已成功设置了ViewBag值抛出的原始异常前,用一个空ViewBag一个新的上下文中使用。

It appears that even though my action filter has successfully set the value in the ViewBag before the original exception was thrown, a new context with an empty ViewBag is used when rendering my Error.cshtml view.

有没有办法让一个动作过滤器创建的数据可自定义错误的看法?

Is there any way to get data created by an action filter to be available to a custom error view?

推荐答案

我想出了通过增加另一个过滤器的我自己的解决方案。

I have come up with my own solution through the addition of another filter.

public class PreserveViewDataOnExceptionFilter : IExceptionFilter
{
    public void
    OnException(ExceptionContext filterContext)
    {
        //  copy view data contents from controller to result view
        ViewResult viewResult = filterContext.Result as ViewResult;
        if ( viewResult != null )
        {
            foreach ( var value in filterContext.Controller.ViewData )
            {
                if ( ! viewResult.ViewData.ContainsKey(value.Key) )
                {
                    viewResult.ViewData[value.Key] = value.Value;
                }
            }
        }
    }

    public static void
    Register()
    {
        FilterProviders.Providers.Add(new FilterProvider());
    }

    private class FilterProvider : IFilterProvider
    {
        public IEnumerable<Filter>
        GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            //  attach filter as "first" for all controllers / actions; note: exception filters run in reverse order
            //  so this really causes the filter to be the last filter to execute
            yield return new Filter(new PreserveViewDataOnExceptionFilter(), FilterScope.First, null);
        }
    }
}

这个过滤器需要在全球范围内的Global.asax.cs中被钩住的Application_Start()致电 preserveViewDataOnExceptionFilter.Register(方法)

This filter needs to be hooked in globally in the Global.asax.cs Application_Start() method by calling PreserveViewDataOnExceptionFilter.Register().

我在这里所做的是建立,去年运行新的异常过滤器,该过滤器HandleErrorAttribute运行后,并复制ViewData的集合,是提供给抛出异常到由创建结果控制器的内容在HandleErrorAttribute过滤器。

What I've done here is to set up a new exception filter that runs last, after the HandleErrorAttribute filter runs, and copies the contents of the ViewData collection that was available to the controller that threw the exception into the result created by the HandleErrorAttribute filter.

这篇关于我如何可以使用过滤器在我Error.cshtml观点放到ViewBag数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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