相同的动作过滤器针对不同的动作 [英] same action filter on different action

查看:79
本文介绍了相同的动作过滤器针对不同的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个自AuthorizeAttribute继承的自定义授权过滤器.经过研究,我发现动作过滤器已缓存,因此只能实例化一次.

I'm implementing a custom authorize filter that inherits from AuthorizeAttribute. After my research I found out action filters are cached so they are instantiated only once.

这是我的问题.如果我实现并使用如下所示的自定义操作过滤器,则该过滤器将无法正常工作,因为它将被实例化一次,并且永远不会再次调用构造函数.但是当我测试时,效果很好,所以我在想一些我不知道的东西.

Here is my question. If I implement and use a custom action filter like below, it shouldn't work correctly because it would be instantiated once and never call constructor again. But when I tested, it worked well so I'm thinking there is something I don't know.

谁能解释清楚这个(动作过滤器生命周期?)?

Can anyone explain this(action filter life cycle?) clearly?

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
  private readonly string value = string.Empty;

  public CustomAuthorizeAttribute(string value)
  {
     this.value = value;
  }

  protected override bool AuthorizeCore(HttpContextBase httpContext)
  {
     // Do something with this.value
  }
}

public class HomeController : Controller
{
  [CustomAuthorize("ACCESS_INDEX")]
  public ActionResult Index()
  {
  }

  [CustomAuthorize("ACCESS_LOGIN")]
  public ActionResult Login()
  {
  }
}

推荐答案

不要这样做.

以我自己的经验,在Action上使用私有变量是不可靠的,即使它有时似乎起作用,您最终还是会有不确定性.

In my own experience, using private variables on the Action is not reliable, even if it sometimes appears to work, you'll likely end up with something non-determinant.

请参见,您的代码可能只对1个请求有效,但在同时处理多个请求时根本不起作用.

See, your code might work fine with 1 request, but not work at all when multiple request are handled simultaneously.

此用户的问题声称与您的体验完全相反:如何使用ActionFilterAttribute记录运行时间?

This user's problem is claiming the exact opposite experience as you: How to use ActionFilterAttribute to log running times?

我唯一的解释是动作调用者是否实例化了动作过滤器,或者是:它不会保持很长时间,或者创建了一个实例池(或两者都有).

My only explanation is if the Action Invoker instantiates the action filter, and that either: it doesn't keep it around very long, or that it create a pool of instances (or both).

我有一个原型,将上下文保留为私有操作属性,偶尔(并非每次)抛出与并发使用相关的EF错误(这是EF的问题.)

I had a prototype that kept a context as a private action property, and it would occasionally (not every time) throw EF errors that were related to concurrent usage ( which was an issue for EF.)

这告诉我,我的动作已被多次使用,并且同时被使用.

This tells me that my action was being used more than once and at the same time.

我建议您将他们的操作重点放在使用过滤器上下文方面.过滤器上下文包含此请求当前正在执行的所有操作.在MVC中,您可以使用 filterContext.HttpContext.Items 存储用于此特定请求的项目.(请参阅在Controller Action中访问Action Filter的数据)

I would recommend that one focus their actions as working with the filter context. The filter context contains everything that is going on right now, for this request. In MVC you can utilize the filterContext.HttpContext.Items to store items that are being utilized for this particular request. ( See Accessing Action Filter's data in Controller Action )

最后,事实也证明,不同版本的MVC框架在不同程度上优化了动作过滤器的生命周期.

Lastly, it could also turn out that different version of the MVC framework optimize the lifecycle of action filters differently.

有关该主题的几个有用链接:

有关一般MVC生命周期的一些详细信息 http://blog.christopheargento.net/2012/06/11/detailed-life-cycle-of-an-asp-net-mvc-request/

Some Details on the general MVC lifecycle http://blog.christopheargento.net/2012/06/11/detailed-life-cycle-of-an-asp-net-mvc-request/

在谈论如何创建动态动作过滤器(通过编程动态地插入和更改)时,Dino Esposito对正在发生的事情进行了一些研究 https://msdn.microsoft.com/en-us/magazine/gg309182.aspx

In talking about how to create Dynamic Action filters (programmatically inserted and changed on the fly), Dino Esposito looks into some underworking of what's going on https://msdn.microsoft.com/en-us/magazine/gg309182.aspx

自定义操作过滤器和多个过滤器订单(如果存在) http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-custom-action-filters

Custom Action Filters, and Filter Order when multiple present http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-custom-action-filters

这篇关于相同的动作过滤器针对不同的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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