2

在一个新创建的 MVC4 应用程序中,将此函数插入​​到帐户控制器中

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AdminLogin(AdminLoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login("administrator", model.Password, persistCookie: model.RememberMe))
        {
            return RedirectToLocal(returnUrl);
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The password provided is incorrect.");
        return View(model);
    }

还有这个

public class AdminLoginModel
{ 
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

放入accountModel.cs。我还创建了一个新文件 AdminLogin.cshtml 并将其留空。在 _loginPartial.cshtml 文件中,我插入了一个操作链接

<li>@Html.ActionLink("Register", "AdminLogin", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>

但是,当我单击该注册链接时,我会看到 404 错误指出 /Account/AdminLogin未找到。

我在插入那个微小的 mvc 的过程中错过了一些东西;你可以帮帮我吗 ?我是一个 mvc 初学者。

4

1 回答 1

3

单击浏览器中的链接会产生 GET 请求,但您的操作方法仅适用于 POST 请求。

添加[HttpGet]属性或删除[HttpPost]属性以解决此特定问题。

通常,您会希望在提交数据时继续使用 POST 请求。因此,我的建议是将客户端更改为使用表单(或使用客户端逻辑来拦截链接单击操作并使用 ajax 请求提交数据)。

于 2013-02-02T20:28:56.047 回答