简体   繁体   English

MVC筛选器ViewResult

[英]MVC Filter ViewResult

I have some filters set up on an MVC C# framework. 我在MVC C#框架上设置了一些过滤器。 From here I am try to render an error page. 从这里,我尝试呈现一个错误页面。 The error page renders correctly, but I want to pass data from the HandleUnautorizedRequest (that depends on which filter you fail) so far I have this. 到目前为止,错误页面可以正确显示,但是我想传递来自HandleUnautorizedRequest的数据(取决于失败的过滤器),到目前为止,我已经做到了。 Is there any way to do something like this, but pass data over to the error page I have in shared. 有什么办法可以做这样的事情,但是将数据传递给我共享的错误页面。 I have already unsuccessfully tried to use ViewData in the object constructor, but I might have just been doing it wrong. 我已经尝试在对象构造函数中使用ViewData失败,但是我可能做错了。

The way our code base is structured I can't initialize any of my controllers from here either. 我们的代码库的结构方式我也无法从此处初始化任何控制器。

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
    filterContext.Result = new ViewResult
    { 
        ViewName = "Error"
    };
}

Not sure how you tried to use ViewData, but you could give this a shot: 不确定如何尝试使用ViewData,但是可以尝试一下:

filterContext.Result = new ViewResult()
                                   {
                                       ViewName = "test",
                                       ViewData = new ViewDataDictionary()
                                                      {
                                                          { "key", "value"}
                                                      }
                                   };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM