C# ASP.NET MVC 4-登录主页
我想在我的主页上的登录表单。在index.cshtml中,我有以下内容:C# ASP.NET MVC 4-登录主页,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我想在我的主页上的登录表单。在index.cshtml中,我有以下内容: @using (Html.BeginForm("Login", "AccountController", FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <div> <div class="usernametb">
@using (Html.BeginForm("Login", "AccountController", FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<div class="usernametb">
@Html.LabelFor(m => m.LoginMod.UserName)
</div>
<div class="usernametb">
@Html.TextBoxFor(m => m.LoginMod.UserName, new { style = "width:150px" })
@Html.ValidationMessageFor(m => m.LoginMod.UserName)
</div>
</div>
<div class="clear"></div>
<div>
<div class="pwtb">
@Html.LabelFor(m => m.LoginMod.Password)
</div>
<div class="pwtb">
@Html.PasswordFor(m => m.LoginMod.Password, new { style = "width:150px" })
@Html.ValidationMessageFor(m => m.LoginMod.Password)
</div>
</div>
<div class="clear"></div>
<div class="rememberme">
@Html.CheckBoxFor(m => m.LoginMod.RememberMe)
@Html.LabelFor(m => m.LoginMod.RememberMe, new { @class = "checkbox" })
</div>
<div><input class="loginbutton" type="submit" value="Log In" /></div>
}
我试图点击这个帐户控制器登录操作,但它从未点击控制器,它只是重新定向到不存在的/AccountController/login。此外,验证控件不起作用
namespace memsite.Controllers
{
[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
我是MVC的新成员,永远离开了webforms land。在表单中使用Account而不是AccountController。ASP MVC基于命名约定,并且隐含控制器。在表单中使用Account而不是AccountController。ASP MVC基于命名约定,并且隐含控制器。Web.config中的“身份验证”部分指定您的登录类型:
<authentication mode="Forms">
<forms loginUrl="Account/Login" timeout="2880" />
</authentication>
可能您的Web.config配置错误。Web.config中的“身份验证”部分指定您的登录类型:
<authentication mode="Forms">
<forms loginUrl="Account/Login" timeout="2880" />
</authentication>
可能您的Web.config配置错误。更改下面一行中控制器的名称
@using (Html.BeginForm("Login", "AccountController", FormMethod.Post)) {
到
由于ASP.NET MVC将通过路由配置自动检测以Controller结尾的任何内容,请更改下面一行中的Controller名称
@using (Html.BeginForm("Login", "AccountController", FormMethod.Post)) {
到
因为ASP.NET MVC将通过路由配置自动检测以Controller结尾的任何内容谢谢。我现在正在打高尔夫球,但稍后会测试并接受答案。谢谢。我现在正在打高尔夫球,但稍后会测试并接受答案