2017-04-20 4 views
0

ここでの仕事: Web.Configの特定のキーに基づいて特定のURL「コントローラ/アクション」を削除したい 私はカスタムフィルタを作成しようとしました属性が、私は「OnActionExecutingが無限ループ の原因」という別の問題を発見し、実際に私は、このソリューション「ASP.NET MVC 3 OnActionExecuting causes infinite loop」で確信していたが、私はまだ解決策を見つけることができませんカスタム属性|| Web.Config内の特定のURLにアクセスするのを防ぐ

のWeb.Config:

<add key="Delegation" value="true" /> 

私のコントローラー:ログインユーザーが承認されているかどうかを確認し、このユーザーがこのコントローラーの資格があるかどうかを確認します。

[MyAuthorize("EdgeEngineGroups")] 
[Edge.Models.FilterAttribute] 

マイろ過クラス:

public class FilterAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     string Delegation = ""; 
     Delegation = System.Configuration.ConfigurationManager.AppSettings["Delegation"].ToString(); 
     if (string.IsNullOrEmpty(Delegation) != null) 
     { 
      if(Delegation.ToLower() == "true") 
      { 
       var controllerName = filterContext.RouteData.Values["controller"]; 
       var actionName = filterContext.RouteData.Values["action"]; 

        filterContext.Result = new RedirectToRouteResult(
         new RouteValueDictionary{{ "controller", controllerName }, 
             { "action", actionName } 

            }); 
      } 
      else 
      { 
       filterContext.Result = new RedirectToRouteResult(
       new RouteValueDictionary{{ "controller", "AccessDenied" }, 
             { "action", "NotFound" } 

            }); 

      } 
     } 
     else 
     { 
      filterContext.Result = new RedirectToRouteResult(
      new RouteValueDictionary{{ "controller", "AccessDenied" }, 
             { "action", "NotFound" } 

            }); 

     } 
     base.OnActionExecuting(filterContext); 
    } 
} 

それはキーが「偽」である右のとき、それが見つからないページにリダイレクトしますが、キーは真であるとき、それは私にリダイレクト作品コントローラーが毎回フィルター属性を検出します。

このエラーを修正する方法があるか、メインタスクの別の解決策があるかどうかを知りたいと思います。

答えて

0

キーが真の場合、同じアクションにリダイレクトされ、同じアクションが属性を呼び出すと再び呼び出され、ループが無限ループになるので、ロジックを変更するときに何もする必要はありません下の関数に渡すだけであなたのコードの完全な例です。それを試してみてください。

public class FilterAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     string Delegation = ""; 
     Delegation = System.Configuration.ConfigurationManager.AppSettings["Delegation"].ToString(); 
      if(string.IsNullOrEmpty(Delegation) || Delegation.ToLower() == "false") 
      { 
       filterContext.Result = new RedirectToRouteResult(
     new RouteValueDictionary{{ "controller", "AccessDenied" }, 
            { "action", "NotFound" } 

           }); 

      } 
     base.OnActionExecuting(filterContext); 
    } 
} 
関連する問題