ASP.NET MVC 4 默认 _LoginPartial 模板注销不起作用
Posted
技术标签:
【中文标题】ASP.NET MVC 4 默认 _LoginPartial 模板注销不起作用【英文标题】:ASP.NET MVC 4 Default _LoginPartial template Logout not working 【发布时间】:2013-12-19 08:55:39 【问题描述】:我一直在摆弄默认 MVC 4 模板的 _Layout 和 _PartialLayouts,突然 '_PartialLogin' 文档中的 'Logout' 功能停止工作。为了提供更多信息,_LoginPartial.cshtml 是从 _NavBar.cshtml 调用的,而 _NavBar.cshtml 又是从 _Layout.cshtml 调用的
_LoginPartial.cshtml 的代码是:
@if (Request.IsAuthenticated)
<text>
<p>Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new @class = "username", title = "Manage" )!
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new id = "logoutForm" ))
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
</p>
</text>
_NavBar.cshtml 中的代码是:
<form class ="navbar-form navbar-right" method="post" action="/account/login">
<div class ="form-group">
@Html.Partial("_LoginPartial")
</div>
</form>
_Layout.cshtml 中的代码是:
<body>
@Html.Partial("_Navbar")
@RenderSection("featured", required: false)
@RenderBody()
<div class="container">
我知道问题可能是 javascript 已被无意中以某种方式禁用。我可能错过了一些脚本标签吗?或者是我在上一课中调用 _PartialLogin 的地方吗??
【问题讨论】:
是否还有一个ID为logoutForm
的表单在页面上执行一个登出操作?
是的。我只更改了“_Layout”表单和“_PartialLogin”表单中的一些行
@HenkMollema 请帮助 :)
你能显示(至少相关部分)布局文件吗?另外,您收到什么错误?
@HenkMollema 没有错误,当我单击注销链接时它没有响应 - 尽管我什至将其更改为 @Html.ActionLink("Log off", "LogOff", "Account", routeValues: null, htmlAttributes: new @class = "btn btn-warning" )
并且重定向不起作用。是因为我在从 _Layout.cshtml 调用的 _NavBar.cshtml 中调用 _LoginPartial.cshtml 吗?
【参考方案1】:
我没有回答你的问题(你已经解决了问题)但我认为这大大改进了代码:
改变这一行:
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
用这个:
<button class="btn btn-link">Log off</button>
此更改也不需要表单上的 id。
【讨论】:
【参考方案2】:我也有这个问题 - 因为我们也在使用引导程序。
这在 Chrome 中运行良好:
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
但要让它在 Explorer 上运行,解决方案是将 javascript 移动到 onclick 属性:
<a href="#" onclick="javascript:document.getElementById('logoutForm').submit()">Log off</a>
https://***.com/a/17078223
【讨论】:
【参考方案3】:解决了问题...
您不能将 javascript 文本放在 <form>
标记中。它将无法工作,因为引导程序会弄乱它。
在彼此之间调用partialLayouts 的次数无关紧要 - 请注意您在其中嵌入了 javascript 的标签
【讨论】:
以上是关于ASP.NET MVC 4 默认 _LoginPartial 模板注销不起作用的主要内容,如果未能解决你的问题,请参考以下文章
ASP.Net MVC 3/4 托管在 IIS 7.5 默认处理程序映射上
ASP.NET MVC Filters 4种默认过滤器的使用附示例
ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 14. ASP.NET Core Identity 入门