Razor MVC部分视图回发调用错误的方法
我有一个Html.FormBegin助手方法的以下部分视图Razor MVC部分视图回发调用错误的方法,razor,asp.net-mvc-5,Razor,Asp.net Mvc 5,我有一个Html.FormBegin助手方法的以下部分视图 @if (Session["UserSpecific"]!=null) //(Request.IsAuthenticated) { using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" })) { @Html.AntiForgeryToken()
@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>
}
}
我不明白为什么它会再次通过LogIn方法运行,因为Html.BeginForm方法指定它将在Account controller中使用注销方法
被调用的登录方法:
// 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上的Index action方法似乎没有标记[AllowAnonymous]属性,这意味着当浏览器在注销后尝试访问它时,它会再次重定向到登录页面,因为您尝试访问的页面只有经过身份验证的用户才能访问
尝试添加[AllowAnonymous]:
我刚刚意识到了一些有趣的事情,我的MVC区域在account controller中调用index方法而不是调用logoff方法时遇到了同样的问题。 我意识到问题在于在应用程序的管理区域执行的路由有缺陷。
试着测试一下,看看你是否能够纠正它 您的HomeController或HomeController上的just the Index action方法是否具有[AllowAnonymous]属性?Index action方法已标记为AllowAnonymous,但当我在代码中插入中断符时,登录操作方法仍被命中,这个部分是在主视图中的标记内呈现的,而不是应该根据Html.BeginForm方法调用的注销操作方法?嵌套表单无效且不受支持这没有帮助,当我在AccountController.cs中放置断点时,它的注销方法仍然没有被命中,但标记为[AllowAnonymous]的登录仍然被命中。这是因为您的表单配置为FormMethod.Post,但您已注释掉[HttpPost]你的注销行动方法的属性?哎呀,让我来解决。在我发布并在代码中更改了它之后,我注意到了它,但忘了在此处更改它。您是否尝试使用浏览器开发工具网络选项卡查看请求/Account/LogOff是否返回了错误?我不熟悉这些工具,因此我将研究它们。但是,当我浏览到/Account/LogOff时,它返回一个错误,即视图不存在,而它不存在。我的理解是,当单击相应的按钮时,BeginForm方法将调用它提供的任何操作方法。这是错的吗?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
//AuthenticationManager.SignOut();
Session.Remove("UserSpecific");
Session["UserSpecific"] = null;
return RedirectToAction("Index", "Home");
}
public class HomeController : Controller {
[AllowAnonymous] // <-- add this
public ActionResult Index() {
return View();
}
// other stuff
}