我有以下部分視圖與Html.FormBegin助手方法;MVC部分視圖回傳調用錯誤的方法
@if (Session["UserSpecific"]!=null) //(Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
@* @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })*@
<input type="button" class="menu-button nav navbar-nav navbar-right" data-target=".customer-list" data-toggle="collapse" />
<div class="customer-list-container">
<div class="customer-list collapse">
<ul>
@{
string[] array = { "Steel Company", "Recycling Company", "Other Company", "Random Name Here" };
}
@foreach (string name in array)
{
<li>@Html.ActionLink(name, "", "", routeValues: null, htmlAttributes: new { @class = "customer-link" })</li><hr/>
}
<li><input type="submit" /></li>
</ul>
<div class="col-xs-6">@Html.ActionLink("Settings", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })</div><div class="col-xs-6"><!--<input type="submit" value="Log Off"/>-><!--<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></div><div class="clearfix">--></div>
</div>
</div>
}
}
的問題是,每當我點擊的不是註銷提交按鈕,它通過登錄方法再次運行,並修改網址爲:
Account/Login?ReturnUrl=%2FAccount%2FLogOff
我不得到爲什麼它再次通過LogIn方法運行,因爲Html.BeginForm方法指定它將使用Account控制器中的LogOff方法。
登錄方法被調用:
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
註銷方法應該被稱爲:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
//AuthenticationManager.SignOut();
Session.Remove("UserSpecific");
Session["UserSpecific"] = null;
return RedirectToAction("Index", "Home");
}
你的'HomeController'或者'HomeController'上的'Index'操作方法是否具有'[AllowAnonymous]'屬性? – BG100
Index action方法已經標記爲AllowAnonymous,但是當我在代碼中放置一箇中斷時,而不是LogOff操作方法(應該根據Html.BeginForm方法調用)時,Login操作方法仍然被打中。 –
這部分被渲染在主視圖中的'