在Asp.net Mvc 5中,如果两个账户控制器和区域重定向到错误的登录页面,可能是因为配置或代码中出现了问题。以下是一些可能的解决方法,包含代码示例:
RouteConfig.cs文件中的RegisterRoutes方法中检查路由配置。例如:routes.MapRoute(
name: "Account",
url: "Account/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "YourProjectNamespace.Areas.Account.Controllers" }
);
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
// 验证登录逻辑 ...
// 如果验证成功,重定向到指定页面
if (success)
{
return Redirect(returnUrl ?? "/");
}
else
{
ModelState.AddModelError("", "登录失败");
}
}
// 如果验证失败,返回登录页面
return View(model);
}
AreaRegistration.cs文件中,确保区域的默认控制器正确设置。例如:public class AccountAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Account";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Account_default",
"Account/{controller}/{action}/{id}",
new { area = "Account", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "YourProjectNamespace.Areas.Account.Controllers" }
);
}
}
Html.ActionLink生成登录链接:@Html.ActionLink("登录", "Login", "Account", new { area = "Account" }, null)
通过检查以上配置和代码,您应该能够解决重定向到错误登录页面的问题。如果问题仍然存在,请检查其他可能的配置或代码错误,并确保正确地处理登录逻辑和重定向逻辑。