如何将标志值传递给Asp.net MVC中的自定义操作筛选器

时间:2014-07-29 09:32:35

标签: c# asp.net-mvc-4 actionfilterattribute

我有使用客户操作过滤器属性的操作方法" FeatureAuthenticationAttribute",我想将标志值传递给过滤器;

如果传递标志值为false,则应重定向到FeatureDenied Action Method。为此:

[FeatureAuthenticationAttribute(flagvalue)]
public ActionResult Jobs()
{
    return View();
}

public ActionResult FeatureDenied()
{
    return View();
}

对于过滤器:

[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class FeatureAuthenticationAttribute : FilterAttribute, IAuthorizationFilter
{
    public override void OnAuthorization (AuthorizationContext filterContext,bool flagvalue)
    {
        if (flagvalue== false) // I want to check here
        {
            string redirectURL = @"~/Employer/FeatureDenied";// Redirect to Action Method 

            filterContext.Result = new RedirectResult(redirectURL);
        }
    }
}

有可能像上面那样吗?如果是这样,我正在使用Filter属性来实现或执行此操作。请帮帮我。

1 个答案:

答案 0 :(得分:0)

你可以在Controller中制作这样的routedata:

RouteData.Values.Add("flag", true/false);

过滤:

 [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
 public class FeatureAuthenticationAttribute : FilterAttribute, IAuthorizationFilter
 {
  public override void OnAuthorization (AuthorizationContext filterContext,bool flagvalue)
   {
        if (filterContext.RouteData.Values["flag"]== false) // I want to check here
        {

            string redirectURL = @"~/Employer/FeatureDenied";// Redirect to Action Method 

            filterContext.Result = new RedirectResult(redirectURL);

        }

    }
 }
相关问题