【问题标题】:Can't remove controller name MVC4无法删除控制器名称 MVC4
【发布时间】: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


    【解决方案1】:

    请确保您获得正确的网址或出于测试目的仅使用硬编码的网址 并尝试一次

    var logoutUrl = '/Account/LogOut';

    PS:如果可以的话,我会发表评论,但声誉会降低

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-15
      • 2022-12-17
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多