繁体   English   中英

ASP.NET MVC 5 > AccountController > 登录 > HTTP 404

[英]ASP.NET MVC 5 > AccountController > Login > HTTP 404

我正在使用 Adam Freeman 的名为“Pro ASP.NET MVC”的书,并尝试实现第 12 章,一个简单的 web 身份验证应用程序。

此 web 应用程序使用:

  1. Login.cshtml ( ~/Account/Login );
  2. AccountController.cs
  3. Web.config

您可以在此处查看Login.cshtml

@model SportsStore.WebUI.Models.LoginViewModel

@{
    /*
    chapter 12 - SportsStore: Security & Finishing Touches
        Securing the Administration Controller
            Creating the View 312
                Listing 12-9. The Contents of the Login.cshtml File
   */

    ViewBag.Title = "Admin: Log In";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

<div class="panel">
    <div class="panel-heading">
        <h3> Log In</h3>
    </div>
    <div class="panel-body">
        <p class="lead">Please log in to access the administration area:</p>
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary()
            <div class="form-group">
                <label>User Name:</label>
                @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
            </div>
            <div class="form-group">
                <label>Password:</label>
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
            <input type="submit" value="Log in" class="btn btn-primary" />
        }
    </div>
</div>

这是AccountController class:

using System.Web.Mvc;
using SportsStore.WebUI.Infrastructure.Abstract;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    /*
    chapter 12 - SportsStore: Security & Finishing Touches
        Securing the Administration Controller
            Creating the Account Controller 312
                Listing 12-8. The Contents of the AccountController.cs File
     */
    public class AccountController : Controller
    {
        IAuthProvider authProvider;

        public AccountController(IAuthProvider auth)
        {
            authProvider = auth;
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (authProvider.Authenticate(model.UserName, model.Password))
            {
                return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
            }
            else
            {
                return View();
            }
        }
    }
}

这是web.config文件:

<system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />

    <!--
    chapter 12 - SportsStore: Security & Finishing Touches
        Securing the Administration Controller
            Creating a Basic Security Policy
                Listing 12-1. Configuring Forms Authentication in the Web.config File
                Listing 12-2. Defining a Username and Password in the Web.config File                   
    -->

    <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2880">
            <credentials passwordFormat="Clear">
                <user name="admin" password="secret" />
            </credentials>
        </forms>
    </authentication>
</system.web>

也许我的错误在这里? RouteConfig/RegisterRoutes

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    null,
        "",
        new { controller = "Product", action = "List", category = (string)null, page = 1 }
    );

routes.MapRoute(
    null,
    "Page{page}",
    new { controller = "Product", action = "List", category = (string)null },
    new { page = @"\d+" }
);

routes.MapRoute(
    null,
    "{category}",
    new { controller = "Product", action = "List", page = 1 }
);

routes.MapRoute(
    null,
    "{category}/Page{page}",
    new { controller = "Product", action = "List" },
    new { page = @"\d+" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

当我运行 web 应用程序时,我收到此 HTTP 404 消息

应用程序“/”中的服务器错误

无法找到该资源。
请求 URL:帐户/登录

我的错误是什么?

您在 AccountController 上有一个采用 HttpPost 的 Login 方法,但为了获得视图,您还需要一个 HttpGet 端点:

[HttpGet]
public ActionResult Login(string returnUrl)
{
    return View();
}

当然用适当的 Login.cshtml 来显示登录表单。

您的 AccountController 只有一种方法(登录)并且它具有 HttpPost 属性。 我假设根据您的描述,您只需转到 /Account/Login 即可在 web 浏览器中点击端点。 您获得 404 的原因是您没有与 HttpGet 属性一起使用的 Login 方法。

如果您创建类似的内容:

[HttpGet]
public ActionResult Login()
{
    return Content("Hi there!");
}

当您尝试在浏览器中点击端点只是为了测试并查看您是否可以点击端点时,您会得到“您好”。

我建议查看有关 MVC 视图的 MS Docs 以及如何使用它们来帮助您解决问题 - https://docs.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-6.0

多谢你们,

由于您的帮助,问题已解决,我将向您展示代码中的修改:

using System.Web.Mvc;
using SportsStore.WebUI.Infrastructure.Abstract;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    /*
    chapter 12 - SportsStore: Security & Finishing Touches
        Securing the Administration Controller
            Creating the Account Controller 312
                Listing 12-8. The Contents of the AccountController.cs File
     */
    public class AccountController : Controller
    {
        IAuthProvider authProvider;

        public AccountController(IAuthProvider auth)
        {
            authProvider = auth;
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (authProvider.Authenticate(model.UserName, model.Password))
            {
                return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
            }
            else
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
    }
}

暂无
暂无

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

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