继承自定义Actionfilterattribute在第二次调用时保留属性的值

时间:2016-11-04 06:42:08

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

我有两个actionfilterattributes和一个动作

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,值仍然是真的。那是准确的吗?如何确保将其重置为我在Actionresult上修饰时传递的原始值或者为false?

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

EDIT2:

只是扩大更多。我们假设我使用按钮转到某个页面,该按钮可以对某个操作执行发布请求。但只有在您被订阅的情况下才能访问该操作。让我们来看看这个模型

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

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

所以我将

public class 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);
   }
}

1 个答案:

答案 0 :(得分:0)

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

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;
        }
    }