使用RedirectToRouteResult重定向不适用于其他区域

时间:2011-08-01 14:59:09

标签: asp.net-mvc-3

我有这个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" }));
        }
    }
}

它从起始页面起作用,它不在一个区域下,它不能在一个区域工作,因为它将重定向到/ Area / Home / Appoffline而不是/ Home / AppOffline

可以修复吗?

还有一种方法可以解决使用泛型和强类型代码重定向到哪个控制器/操作?

2 个答案:

答案 0 :(得分:54)

尝试将area路由令牌分配给空字符串:

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
    action = "AppOffLine", 
    controller = "Home", 
    area = "" 
}));

答案 1 :(得分:13)

你必须指定这样的区域:

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",
                          area = "YourAreaName" })); //<<<<THIS
        }
    }
}

如果要重定向到非区域区域(如/Home/Index),请将“area”设置为空字符串;像:

area=""