【发布时间】:2019-10-04 11:48:51
【问题描述】:
编辑了问题,因为我发现问题不在剃须刀内部,而是在路线中
我有一个非常简单的登录表单,但是不知何故,当用户按下登录时,页面进入 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>
}
在我的登录控制器中,我有一个名为 Verify 的简单 ActionResult,带有 2 个参数。
[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)
{...}
我到底做错了什么?这不像是火箭科学。
编辑2: 我注意到,每当我将 RouteConfig.cs 更改回默认值时,它都能正常工作。因此,问题不在表单标签内,而在路由内。 因此,我一直在尝试添加自定义路线,以便使用此示例使其正常工作:Using Html.BeginForm() with custom routes
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 }
);
}
}
当我删除路由并将所有内容恢复为默认值时,控制器确实会受到影响。不幸的是,我真的需要这些路由用于应用程序的其余部分:(
【问题讨论】:
标签: c# asp.net-mvc razor routes