c# - Redirect From Action Filter Attribute -


What is the best way to redirect a ActionFilterAttribute ? I have ActionFilterAttribute named "IsAuthenticatedAttributeFilter" and checked the value of a session variable. If the variable is wrong, then I want to redirect the application to the login page. I prefer to redirect using the root name SystemLogin , but at this point any redirect method will be cured.

, root value);

You can do anything like this:

  filterContext.Result = new ViewResult {ViewName = SharedViews.SessionLost, ViewData = filterContext.Controller.ViewData} ;   

If you want to use RedirectToAction :

then you have a public RedirectToAction method You can create your controller ( preferably on your base controller ) which only calls secure RedirectToAction to System.Web.Mvc.Controller . Adding this method allows public call your redirect action from the filter.

  Redirect the new redirect reader redirects action (string action, string controller) {return base. Redirection projection (action, controller); }   

Then your filter will look something like this:

  Public override zero on anchoring (Action Accepting Contact Filter Contact) {var Controller = (some control) filterContext.Controller; FilterContext.Result = controller.RedirectToAction ("index", "home"); }    

Comments