Reputation: 4964
I'm working on a MVC 4 project with a user login page. It all works fine.
The issue is when i click on the link to log out, the redirect URL is incorrect.
Here is the code to explain better:
View:
<a href="@Url.Action("LogOff", "Account")">Logout ?</a>
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
WebSecurity.Logout();
return RedirectToAction("Login", "Account");
}
When I click on the log out link, the URL show me this ==> Account/LogOff
But I want to redirect to this ==> Account/Login
Does anyone know what is going on?
Upvotes: 2
Views: 125
Reputation: 1785
Remove HttpPost attribute and everything should be fine.
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
WebSecurity.Logout();
return RedirectToAction("Login", "Account");
}
Upvotes: 2
Reputation: 1038720
In HTML an anchor element (<a>
) sends GET request, not POST, so your [HttpPost]
controller action will never be invoked. You should use an HTML form with POST method if you want to invoke this action:
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post))
{
@Html.AntiForgeryToken()
<button type="submit">Logout ?</button>
}
Also notice that I included an AntiForgery token inside the form because your controller action is decorated with the [ValidateAntiForgeryToken]
attribute and thus is expecting it.
Upvotes: 4