I have to restrict access to particular action in a controller and so i have created a RestrictAccess attribute which implements ActionFilterAttribute .
RestrictAccess
public class RestrictAccess : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {{ "Controller", "Test" },
{ "Action", "RedirectToError" } });
base.OnActionExecuting(filterContext);
}
}
While debugging , after executing above code it is supposed to redirect and go hit the TestController's RedirectToError action. But it fails to redirect .
Test Controller:
public ActionResult RedirectToError()
{
return View("AccessRestriction.cshtml");
}
Instead i get this below error in browser.
Endpoint not found. Please see the service help page for constructing valid requests to the service.
Can someone help me in this?
What I have tried:
I have tried overriding unauthorizedRequest as well. Still same issue.
public class RestrictAccess : AuthorizeAttribute
{
public override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext);
filterContext.Result = new RedirectResult(urlHelper.Action("Test", "RedirectToError"));
}
}