【问题标题】:Inherited custom Actionfilterattribute keeps value of the property on second call of action继承的自定义 Actionfilterattribute 在第二次调用操作时保留属性的值
【发布时间】:2016-11-04 06:42:07
【问题描述】:

我有两个 actionfilter 属性和一个动作

public class BaseAttribute : ActionFilterAttribute{

   protected bool _something
   public BaseAttribute(bool something){
       _something = something
   }
   public override void OnActionExecuting(ActionExecutingContext filterContext){
      _something = true;
      Console.WriteLine(_something);
   }
}


public class ChildAttribute : BaseAttribute{
   public ChildAttribute(bool somethingChild): base(somethingChild){
   }

   public override void OnActionExecuting(ActionExecutingContext filterContext){
      Console.WriteLine(_something);

      if(!_something)
           base.OnActionExecuting(filterContext);
   }
}

[ChildAttribute (false)]
public ActionResult SomeMethodCalledByAngular(){
  ......
}

当我第一次调用 SomeMethodCalledByAngular 时,_something 变量会按预期更新......它变为 true,但没有刷新页面并且我再次点击 actionresult,该值仍然为 true。那准确吗?如何确保它重置为我在 Actionresult 上装饰它时传递的原始值或 false?

编辑: 所以我基本上是在尝试根据从数据库中获取的表字段来更新该变量。例如,如果用户没有刷新页面,但该字段的值发生了变化,我想更新变量。该操作过滤器就像请求的安全过滤器一样,因为我使用 API 之类的操作,至少对于特定的控制器。

EDIT2:

只是为了扩展更多。假设我转到一个带有按钮的页面,该按钮可以执行对操作的发布请求。但是只有在您订阅后才能访问该操作。让我们来看看这个模型

public class User{
   public int Id {get;set;}
   public DateTime SubscribedFrom {get;set;}
   public DateTime SubscribedTo {get;set;}
}

基本上,每次触发操作过滤器时,我都需要检查用户是否仍在订阅日期内。如果不是,则不应访问该操作,因此返回错误。

所以我要了

公共类 BaseAttribute : ActionFilterAttribute{

   protected bool _subscribed
   public BaseAttribute(bool subscribed){
       _subscribed= subscribed
   }
   public override void OnActionExecuting(ActionExecutingContext filterContext){
      User user = <get the user details>
      if(user.SubscribedFrom < DateTime.Now && user.SubscribedTo > DateTime.Now)
      _subscribed = true;
      Console.WriteLine(_something);
   }
}


public class ChildAttribute : BaseAttribute{
   public ChildAttribute(bool somethingChild): base(somethingChild){
   }

   public override void OnActionExecuting(ActionExecutingContext filterContext){

      // do a check on another layer of security and see if there's an override. if there is not, _subscribed remains false, then proceeds to the base filter to validate the user subscription

      if(!_subscribed)
           base.OnActionExecuting(filterContext);
   }
}

【问题讨论】:

  • 发生这种情况是因为属性的构造函数只被调用一次并且只要应用程序运行它就存在。基本上它是一个单例。这就是为什么它保持相同的值。告诉我更多关于你想用 _something 变量实现什么。
  • 啊,知道这很有趣!编辑了更多信息。让我知道这是否有意义。谢谢!
  • 感谢您的补充解释,但我恐怕还是不明白。但是,无论您尝试做什么,我都认为这不是一个好方法。问题是您无法保证应用程序将在请求之间启动(因此无法保证保留 _something 的值)。在您的情况下,解决方案可能应该是使用旨在存储请求之间信息的东西,例如会话。
  • 再次更新。我无法进行会话,因为我计划拥有一个可能需要也可能不需要更新订阅属性的管理员帐户。这更多地用于测试,可能会取消订阅。

标签: asp.net-mvc asp.net-mvc-4 model-view-controller action-filter actionfilterattribute


【解决方案1】:

好的,现在我明白了 :) 但是,在我看来,这不是一个干净的设计。问题是你有两个类都使用的这个变量,这个逻辑有点难以推理。但我认为你不需要这个变量来做你正在做的事情。我稍微重新设计了这个。看看这是否适合你:

public class BaseValidationCheckAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            User user = < get the user details >
            if (IsSubscriptionValid(user))
            {
               DoThings(); 
            }
        }

        protected bool IsSubscriptionValid(User user)
        {
            return (user.SubscribedFrom < DateTime.Now && user.SubscribedTo > DateTime.Now);
        }

        protected void DoThings()
        {
            Console.WriteLine("doing something...");
        }
    }

    public class ValidationWithOverrideCheckAttribute : BaseValidationCheckAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            User user = < get the user details >
            //first checking the override and if true we don't even care about the base validation, if false, then we also check the subscription validation 
            if (IsSubscriptionOverride(user) || IsSubscriptionValid(user))
            {
                DoThings();
            }
        }

        private bool IsSubscriptionOverride(User user)
        {
            //here check whatever to see if we should override the base subscription validation
            return false;
        }
    }

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多