MVC 抛出 HTTP 404 错误

本文关键字:错误 HTTP 抛出 MVC | 更新日期: 2023-09-27 17:56:07

我完全陷入困境并困惑为什么它适用于一个视图到控制器而不是另一个视图。

有效的那个:

public class HomeController : Controller
{
    // GET: Home
    IAuthenticationManager Authentication
    {
        get { return HttpContext.GetOwinContext().Authentication; }
    }
    public ActionResult Index()
    {
        return View();
    }
    [POST("login")]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel input)
    {
        if (ModelState.IsValid)
        {
            if (input.HasValidUsernameAndPassword())
            {
                var identity = new ClaimsIdentity(new[] {
                        new Claim(ClaimTypes.Name, input.Username),
                    },
                    DefaultAuthenticationTypes.ApplicationCookie,
                    ClaimTypes.Name, ClaimTypes.Role);
                // if you want roles, just add as many as you want here (for loop maybe?)
                if (input.isAdministrator)
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
                }
                else
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
                }
                // tell OWIN the identity provider, optional
                // identity.AddClaim(new Claim(IdentityProvider, "Simplest Auth"));
                Authentication.SignIn(new AuthenticationProperties
                {
                    IsPersistent = input.RememberMe
                }, identity);
                return RedirectToAction("password", "authentication");
            }
        }
        return View("show", input);
    }
    [GET("logout")]
    public ActionResult Logout()
    {
        Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        return RedirectToAction("login");
    }
}

使用此 CSHTML 代码:

@model AgridyneAllIn1MVC.Models.Authentication.LoginModel
@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()
 <div class="form-horizontal">
    <h4>Details</h4>
    <hr />
    @Html.ValidationSummary(true)
    <div class="form-group">
        @Html.LabelFor(model => model.Username, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.RememberMe, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RememberMe)
            @Html.ValidationMessageFor(model => model.RememberMe)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Login" class="btn btn-default" />
        </div>
    </div>
</div>
}

不起作用的 CSHTML 是:

@using Microsoft.AspNet.Identity
@model AgridyneAllIn1MVC.Models.Authentication.LoginModel
@if(User.Identity.IsAuthenticated)
{
   using (Html.BeginForm("resetpassword", "authentication", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Reset your password.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.Username, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Reset" />
        </div>
    </div>
}
}

随之而来的控制器是:

public class AuthenticationController : Controller
{
    [Authorize]
    public ActionResult Password()
    {
        return View("ResetPassword");
    }
    [Authorize]
    public ActionResult Register()
    {
        return View("Register");
    }
    [Authorize]
    [POST("resetpassword")]
    public ActionResult ResetPassword(LoginModel input)
    {
        if (ModelState.IsValid)
        {
            if (User.Identity.IsAuthenticated)
            {
                if(input.SuccessfullyUpdatedPassword())
                {
                    return View("Password");
                }
            }
        }
        return View("Password");
    }
    [Authorize]
    [POST("registernewuser")]
    public ActionResult RegisterNewUser(LoginModel input)
    {
        if (ModelState.IsValid)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (input.SuccessfullyUpdatedPassword())
                {
                    return View();
                }
            }
        }
        return View();
    }
}

每次我尝试访问这些页面中的任何一个时,我都会得到

找不到资源。HTTP 404。请求的网址:/重置密码。

我是否缺少 HTML 中所需的内容。BeginForm() 会让它转到正确的 ActionEvent 来触发我的代码并更新视图?这是我从头开始构建并从头开始设计视图的第一个 MVC。MVC 在一个控制器上有多个视图时是否有问题?让我感到震惊的是,有效的 MVC 在 HTML 中没有填充任何内容。BeginForm(),我在没有任何HTML的情况下尝试了它。BeginForm() 和我得到相同的结果。我尝试将[GET("重置密码")]更改为[GET(")],但它也不起作用。我有另一个称为寄存器的视图,它与身份验证控制器存在相同的问题。所以有人可以告诉我出了什么问题并解释为什么,这样我就可以完全理解 MVC 希望如何完成工作,以便我为我仍然需要构建的其他 50 多个视图做对。

MVC 抛出 HTTP 404 错误

您的表单使用FormMethod.Post但控制器中不存在POST resetpassword方法

[GET("resetpassword")]更改为 POST,因为 cshtml 文件中的表单使用 post 方法:

using (Html.BeginForm("resetpassword", "authentication", FormMethod.Post,....