解决FilterAttributes控制器和行动 [英] Resolve FilterAttributes On Controller And Action

查看:111
本文介绍了解决FilterAttributes控制器和行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做到这一点:

[RequiresAuthentication(CompanyType.Client)]
public class FooController
{    
     public ActionResult OnlyClientUsersCanDoThis()

     public ActionResult OnlyClientUsersCanDoThisToo()

     [RequiresAuthentication]
     public ActionResult AnyTypeOfUserCanDoThis()

您可以看到为什么这是行不通的。在第三个动作控制器级过滤器会阻止非客户机。我想,而不是为解决冲突的过滤器。我想对于更具体的过滤器(动作过滤器),以总是赢。这似乎是自然和直观。

You can see why this won't work. On the third action the controller-level filter will block non-clients. I would like instead to "resolve" conflicting filters. I would like for the more specific filter (action filter) to always win. This seems natural and intuitive.

曾几何时filterContext暴露的MethodInfo为执行行动。这会作出这样pretty容易。我考虑过使用路由信息做一些反思自己。这是行不通的,因为它的行动可能是超载,我无法分辨出哪一个是执行一个的电流。

Once upon a time filterContext exposed MethodInfo for the executing action. That would have made this pretty easy. I considered doing some reflection myself using route info. That won't work because the action it might be overloaded and I cannot tell which one is the current executing one.

另一种方法是范围过滤器无论是在控制器级别或行动水平,但没有组合,这将创造大量的额外属性的噪音。

The alternative is to scope filters either at the controller level or the action level, but no mix, which will create a lot of extra attribute noise.

推荐答案

我们正在寻找进入的方式揭露其他过滤器,但没有承诺。

We're looking into a way to expose other filters, but no promises.

应用过滤器控制器是不是一个真正的范围,它只是一个短期的手它适用于所有的过滤器。不幸的是,这意味着你不能包括所有,但一个动作。你可以这样做一个简单的方法就是把一个方法,另一个控制器。你甚至可以添加自定义的路由只为那一个情况下使URL没有改变。

Applying a filter to the controller isn't really a "scope", it's merely a short-hand for applying it to all filters. Unfortunately, that means you can't include all but one action. One simple way you could do this is to put that one method in another controller. You could even add a custom route just for that one case so the URL doesn't have to change.

这篇关于解决FilterAttributes控制器和行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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