我有以下项目结构。我在测试区内有两个家庭和帐户控制器,在路线级项目有一个。现在从区域登录后我想重定向区域的主页索引但是它将我重定向到路由级主页索引。
测试区域注册MapRoute
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { controller = "Account", action = "Login", id = UrlParameter.Optional },
namespaces: new[] { "MVCAreaSample.Areas.Test.Controllers" }
);
}
基础级别maproute
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional },
namespaces: new[] { "MVCAreaSample.Controllers" }
);
区域重定向操作方法
[HttpPost]
public ActionResult Login(TestModel test)
{
var candidateContext = "LoginContext";
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(2, candidateContext, DateTime.Now, DateTime.Now.AddMinutes(60), true, "Manjay");
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
//Check required for code contracts.
if (System.Web.HttpContext.Current != null)
{
System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
if (System.Web.HttpContext.Current.Session != null)
System.Web.HttpContext.Current.Session["LoginContext"] = candidateContext;
}
return RedirectToAction("Index", "Home");
}
我尝试在路线中给出区域名称。它适用于这种情况。但是假设我在两个级别都有适当的认证逻辑而不是
RedirectToAction(“Index”,“Home”,new {area =“Test”});
它将为我提供基础级别的登录页面。
答案 0 :(得分:4)
您只需确定现有区域路线并将其作为重定向中的参数传递,如下所示:
return RedirectToAction("Index", "Home", new { area = "" });
要返回某个级别,您只需为该区域指定一个空白字符串:
app.listen