2014-12-31 74 views
-1

我在MVC4工作,我需要在特定场景下将用户重定向到LogOut。我正在使用.js文件中的重定向。但是,当我尝试重定向routing是不正确的。它增加了现有的controller,并把我错误。无法删除控制器名称MVC4

//代码

// .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

我routeConfig,

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

我该如何做到这一点?

回答

0

请确保您得到正确的URL或为测试目的只是使用硬编码网址 并尝试一次

var logoutUrl = '/Account/LogOut';

PS:我会,如果我可以,但口碑不太

评论
相关问题