MVC抛出HTTP 404错误

时间:2015-06-12 15:51:06

标签: c# asp.net-mvc-5

我完全陷入困惑并且困惑为什么它适用于一个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();
    }
}

每当我尝试进入其中任何一个页面时,我都会

  

无法找到资源。 HTTP 404.请求的URL:/ resetpassword。

我是否遗漏了HTML.BeginForm()中需要的东西,它会使它转到正确的ActionEvent以触发我的代码并更新视图?这是我从头开始构建并从头开始设计视图的第一个MVC。 MVC对一个控制器有多个视图有问题吗?让我感到兴奋的是,运行的MVC没有任何填充HTML.BeginForm()的东西,我在HTML.BeginForm()中没有任何东西的情况下尝试了它,我得到了相同的结果。我尝试将[GET(&#34; resetpassword&#34;)]更改为[GET(&#34;&#34;)],但它也无法正常工作。我有另一个称为寄存器的视图,它与AuthenticationController有同样的问题。那么有人可以告诉我出了什么问题并解释为什么这样我就能完全理解MVC是如何完成任务的,所以我对其他50个以上的视图也是如此,我仍然需要构建它。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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