簡體   English   中英

如何將標志值傳遞給Asp.net MVC中的自定義操作篩選器

[英]How to Pass a flag value to Custom Action Filter in Asp.net MVC

我有帶有“自定義操作”過濾器屬性“ 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);
        }
    }
}

有可能像上面一樣嗎? 如果是這樣,我在如何實現或執行此操作的篩選器屬性中感到吃驚。 請幫我。

您可以在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);

        }

    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM