Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect with RedirectToRouteResult does not work from other area

Tags:

I have this ActionFilter

public class AppOfflineFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionDescriptor.ActionName != "AppOffLine" &&
            filterContext.HttpContext.Request.UserHostName != "127.0.0.1")
        {
            filterContext.Result = new RedirectToRouteResult(
                 new RouteValueDictionary(
                     new  { action = "AppOffLine", Controller = "Home" }));
        }
    }
}

It works from the start page which is does not reside under a Area, it does not work from an area because it will redirect to /Area/Home/Appoffline instead of /Home/AppOffline

Can it be fixed?

Also is there a way of speciefing which controller / action to redirect to using Generics and strongly typed code?

like image 263
Anders Avatar asked Aug 01 '11 14:08

Anders


People also ask

How do I redirect an action filter?

If you want to use RedirectToAction : You could make a public RedirectToAction method on your controller (preferably on its base controller) that simply calls the protected RedirectToAction from System. Web. Mvc.

What is redirect to route in MVC?

RedirectToRouteResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. Targets a registered route. It should be used when we want to redirect to a route.

What is the use of redirect to action?

RedirectToAction(String, String, Object)Redirects to the specified action using the action name, controller name, and route dictionary.


1 Answers

Try assigning the area route token to an empty string:

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
    action = "AppOffLine", 
    controller = "Home", 
    area = "" 
}));
like image 134
Darin Dimitrov Avatar answered Sep 18 '22 03:09

Darin Dimitrov