【发布时间】:2014-12-31 06:07:16
【问题描述】:
我在MVC4 工作,我需要在特定场景下将用户重定向到LogOut。我在.js 文件中使用重定向。但是当我尝试重定向 routing 时是不正确的。它添加到现有的controller 并抛出错误。
//代码
// .cshtml:
<script type="text/javascript">
var logoutUrl = '@Url.Action("LogOut", "Account")';
</script>
//控制器:
[AllowAnonymous]
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Login", "Account", null);
}
//.JS:
window.location.href = logoutUrl;
当我尝试重定向时,用户将在页面中
/Home/Index
但是,重定向后它会转到
/Home/Account/LogOut
而不是/Account/LogOut
我的路由配置,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
这引发了我的错误。但我需要从网址中删除Home 并重定向到Account/LogOut。
我怎样才能做到这一点?
【问题讨论】:
标签: asp.net-mvc-4 redirect asp.net-mvc-routing