【问题标题】:How to Pass a flag value to Custom Action Filter in Asp.net MVC如何将标志值传递给 Asp.net MVC 中的自定义操作过滤器
【发布时间】:2014-07-29 09:32:34
【问题描述】:

我有带有自定义操作过滤器属性“FeatureAuthenticationAttribute”的操作方法,我想将标志值传递给过滤器;

如果传递标志值是假的,它应该重定向到 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 Attribute 中发现如何实现或执行此操作。请帮帮我。

【问题讨论】:

  • 这个标志值应该从哪里来?
  • 标志值来自数据库,但这不是问题,我想检查标志是真还是假,如果它是假的,那么它应该重定向到 FeatureDenied Action 方法。

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


【解决方案1】:

您可以在 Controller 中制作这样的路由数据:

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

        }

    }
 }

【讨论】:

  • 只需将来自数据库的标志值放入 routedata 中,如示例第一行所示,然后在操作过滤器中检查其值,如上所示。我认为它应该适合你..@Chandu跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
相关资源
最近更新 更多