2017-10-19 93 views
0

我有以下代码注销并重定向到另一个页面控制器:SignOut()在asp.net MVC 4

... 
public ActionResult Logout(){ 
    FormsAuthentication.SignOut(); 
    return Redirect("Index"); 
} 
... 

我有以下的“母版页”:

... 
<body> 
    <header> 
     <div style="background-color:deepskyblue; width:100%; height:20px"> 
      @if(User.Identity.IsAuthenticated){ 
       <a style="position:relative; float:right; right:15px" href="@Url.Action("Logout", "Home", null)">Logout</a> 
      } 
      else{ 
       <a style="position: relative; float: right; right: 15px" href="@Url.Action("Login", "Home", null)">Login</a> 
      } 
     </div> 
    </header> 
    @RenderBody() 
    <footer> 
     <div style="position:relative; background-color:lightslategrey; width:100%; height:20px"> 
      @if(User.Identity.IsAuthenticated){ 
       <a style="position: relative; float: left; left: 15px" href="@Url.Action("Index", "Home", null)">Go back</a> 
      } 
     </div> 
    </footer> 
</body> 
... 

问题是:当注销操作被调用时,它会重定向到索引页面,但页眉和页脚不会更改,我必须再次单击“注销”或刷新页面,然后才能正常工作。

它如何工作,而不必点击两次或刷新页面?

+0

[Clearout MVC 4上清除会话]的可能重复(https://stackoverflow.com/questions/27738174/clear-session-on-logout-mvc-4) – JuanR

回答

0

我固定它改变这一点:

  @if(User.Identity.IsAuthenticated){ 
       <a style="position:relative; float:right; right:15px" href="@Url.Action("Logout", "Home", null)">Logout</a> 
      } 
      else{ 
       <a style="position: relative; float: right; right: 15px" href="@Url.Action("Login", "Home", null)">Login</a> 
      } 

与此:

  @if(User.Identity.IsAuthenticated){ 
       @Html.ActionLink("Logout", "Logout", "MyAccount", null, new{@style="position:relative; float:right; right:15px"}) 
      } 
      else{ 
       @Html.ActionLink("Login", "Login", "MyAccount", null, new{@style="position: relative; float: right; right: 15px"}) 
      } 

谢谢反正你的答案。

1

为什么在这里使用Redirect()RedirectToAction()是在应用程序上下文中发出302重定向的首选方式。它遵守你的路由。我想那Redirect("Index")不会总是作为Redirect("/Controller/Index")将工作。

return RedirectToAction("Controller", "Index");