0

My Task here: I want to eliminate a specific url "Controller/Action" based on a specific key in "Web.Config I tried to make a custom filter attribute, but I found another problem that the "OnActionExecuting causes infinite loop ", and actually I was convinced by this solution "ASP.NET MVC 3 OnActionExecuting causes infinite loop", but I still can't find a solution.

Web.Config:

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

My Controller: I check if the login user is authorized or not, and then check if this user is eligible for this controller or not.

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

My Filteration Class:

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

It works right when the key is "false", it redirects to the not found page, but when the key is true, it redirects to my controller but it finds the filter attribute every time.

I wanna know if there's a way to fix this error, or another solution to the main task.

Community
  • 1
  • 1

1 Answers1

0

its happening cause when the key is true then u redirecting to the same action and when the same action call the attribute will call again and it will be gonna infinite loop so change the logic don't need to do anything when its true just let it pass to base function below is full example for your code. try it.

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);
    }
}
Umair Anwaar
  • 1,130
  • 9
  • 27