我已經創建了登錄頁面,用戶可以登錄。不,我希望用戶註銷。在索引頁面呼叫操作結果註銷
我有這個在我的AccountController:
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
但我怎麼能稱之爲的ActionResult在我的索引頁?
我已經創建了登錄頁面,用戶可以登錄。不,我希望用戶註銷。在索引頁面呼叫操作結果註銷
我有這個在我的AccountController:
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
但我怎麼能稱之爲的ActionResult在我的索引頁?
有幾個不同的方式,你可以做這
從視圖
@Html.ActionLink("Log Out", "LogOff", "Account")
或
<a href="@Html.Action("LogOff", "Account")">Log Out</a>
或
<a href="@Url.Content("~/Account/LogOff")">Log Out</a>
我不建議的最後一個但是,因爲它沒有考慮到行動和控制器路由。我只在這裏列入它作爲最後的選擇。
從另一個動作
您也可以直接從代碼中調用的動作,就像您的任何普通類的方法,如果你需要做到這一點。像這樣:
class ActionController : Controller
{
// ...
public ActionResult AnotherAction()
{
// Do stuff here
return LogOff(); // You don't have to return the results if they're not needed
}
// ...
}
這樣你可以調用這個ActionResult的
<a href="@Href("~/Account/LogOff")">Logout</a>
保持一個斷點在你註銷等動作在您的賬戶控制器和看發生了什麼事..
其最好的做法作出註銷方法POST方法,所以我建議使用這種形式:
<form method='post'>
<button type='submit'>LogOff</button>
</form>
[HttpPost]
public ActionResult Logoff()
{
//Do LogOff Stuff.
}