[英]SignOut() in asp.net MVC 4
I have the following code to sign out and redirect to another page in the controller: 我有以下代码可以注销并重定向到控制器中的另一个页面:
...
public ActionResult Logout(){
FormsAuthentication.SignOut();
return Redirect("Index");
}
...
I have the following "Master Page": 我有以下“母版页”:
...
<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>
...
The problem is: when the Logout action is called, it redirects to de Index page, but the header and footer do not change, I have to click "Sign out" again or Refresh the page and then it works. 问题是:调用注销操作时,它将重定向到de Index页面,但是页眉和页脚没有更改,我必须再次单击“注销”或刷新页面,然后它才能工作。
How can it work without having to click twice or refreshing the page? 它如何工作而无需单击两次或刷新页面?
Why are you using Redirect()
here? 为什么在这里使用Redirect()
? RedirectToAction()
is the preferred way to issue a 302 redirect within the context of your application. RedirectToAction()
是在应用程序上下文内发出302重定向的首选方法。 It adheres to your routing. 它遵循您的路由。 I would thing that Redirect("Index")
would not always work as Redirect("/Controller/Index")
would. 我想Redirect("Index")
不会总是像Redirect("/Controller/Index")
那样工作。
return RedirectToAction("Controller", "Index");
I fixed it changing this: 我修复了这一问题:
@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>
}
with this: 有了这个:
@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"})
}
Thank you anyways for your answers. 无论如何,谢谢您的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.