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.