表单未命中控制器

Кат*_*ина 10 c# asp.net-mvc routes razor

当我发现问题不在剃须刀内部而是在路线中时,编辑了问题

我有一个非常简单的登录表单,但是以某种方式,当用户按下Login时,页面转到Error404,并且由于某种原因它根本没有达到控制器断点。

   @using (Html.BeginRouteForm("MyCustomRoute", new { controller = "login", action = "verify", FormMethod.Post }))
                 {
                <fieldset class="clearfix">
                    <p><span style="float:none;color:black; font-size:20pt;"></span></p>
                    <p><span style="float:none;color:black; font-size:20pt;"></span></p>
                    <p><span class="fa fa-user"></span>@Html.TextBoxFor(m => m.UserName, new { @class = "form-control", placeholder = "Username", onkeydown = "convertTabtoEnter(this, event)", autofocus = "" })</p> <!-- JS because of IE support; better: placeholder="Username" -->
                    <p>
                        <span class="fa fa-lock"></span>@Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password", onkeyup = "convertTabtoEnter()" })
                    </p> <!-- JS because of IE support; better: placeholder="Password" -->

                    <div>
                        <span style="width:48%; text-align:left;  display: inline-block;">
                            <a class="small-text" href="#">
                                @*Forgot
                    password?*@
                            </a>
                        </span>
                        <span style="width:50%; text-align:right;  display: inline-block;"><input type="submit" value="Sign In"></span>
                    </div>
                </fieldset>
                <div class="clearfix"></div>
                }    
Run Code Online (Sandbox Code Playgroud)

在我的登录控制器内部,我有一个简单的ActionResult,它带有2个参数,名为Verify。

    [RoutePrefix("Login")]

public class LoginController : Controller
{
    // GET: Login
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [Route("Verify")] //Matches GET login/verify

    public ActionResult Verify(string username, string password)
    {...}
Run Code Online (Sandbox Code Playgroud)

我到底在做什么错?这不是火箭科学。

Edit2:我注意到,每当将RouteConfig.cs更改回默认值时,它都能正常工作。因此,问题不在于表单标签之内,而在于路由之内。因此,我一直在尝试添加自定义路由,以使用此示例使此工作正常进行:将Html.BeginForm()与自定义路由一起使用

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "TrailersOverview",
                url: "{TrailersOverview}/{action}/{vendid}",
                defaults: new { controller = "TrailersOverview", action = "Index", vendId = UrlParameter.Optional }
            );

             routes.MapRoute(
                "MyCustomRoute", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "login", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                name: "Default",
                url: "{*anything}",
                defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我删除路由而只是将所有内容恢复为默认值时,控制器就会受到攻击。不幸的是,我在其余的应用程序中确实需要这些路由:(

Mar*_*arc 5

有几件事似乎是错误的:

  • 路由配置中缺少您的控制器。
  • 操作和控制器名称顺序错误。

RouteConfig:

默认路由如下所示:

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

形成:

如果要使用标签助手,则必须从以下位置更改代码:

<form action="@Url.Action("Login", "Verify")" method="post">
Run Code Online (Sandbox Code Playgroud)

至:

<form asp-controller="Login" asp-action="Verify" method="post">
Run Code Online (Sandbox Code Playgroud)

在这里看看。