繁体   English   中英

MVC引发HTTP 404错误

[英]MVC throwing HTTP 404 error

我完全陷入困惑,为什么它只能与一个View to Controller一起使用而不能与另一个视图一起使用。

有效的一种:

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();
    }
}

每次我尝试进入其中两个页面时,我都会得到

无法找到该资源。 HTTP404。请求的URL:/ resetpassword。

我是否缺少HTML.BeginForm()所需的某些东西,它将使它转到正确的ActionEvent来触发我的代码并更新视图? 这是我从头开始构建并从头开始设计视图的第一个MVC。 MVC是否有一个控制器具有多个视图的问题? 我得到的是,有效的MVC在HTML.BeginForm()中没有任何内容,我在HTML.BeginForm()中没有任何内容的情况下进行了尝试,但得到的结果相同。 我尝试将[GET(“ resetpassword”)]更改为[GET(“”)),但它也不起作用。 我有另一种称为注册的视图,它与AuthenticationController有相同的问题。 所以有人可以告诉我哪里出了问题并解释原因,以便我可以完全理解MVC如何完成事情,这样我就可以正确地解决我仍然需要构建的其他50多个视图。

您的Form使用FormMethod.Post但Controller中不存在POST resetpassword方法

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM