Html.BeginForm 的 ASP.Net MVC 路由问题
使用 MVC,我认为有一个 html 表单助手:
using (Html.BeginForm("ActionOne", "ControllerOne")) ...
使用默认路由,操作属性的输出符合预期:
<form action="/ControllerOne/ActionOne" ...
但是注册一个看似没有匹配项的新路由会影响输出。
路由代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("testRoute", new Route("MyUrl", new MvcRouteHandler()));
routes.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index"});
}
输出:
<form action="/MyUrl?action=ActionOne&controller=ControllerOne"
这是设计使然还是我错过了一些基本的东西?
干杯!
Using MVC, I have an html form helper in my view:
using (Html.BeginForm("ActionOne", "ControllerOne")) ...
Using the default route, the output for the action attribute is as expected:
<form action="/ControllerOne/ActionOne" ...
But registrering a new route with seemingly no matches affects the output.
Routing code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add("testRoute", new Route("MyUrl", new MvcRouteHandler()));
routes.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index"});
}
Output:
<form action="/MyUrl?action=ActionOne&controller=ControllerOne"
Is this by design or am I mising something fundamental?
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我经历过这个确切的问题。我不确定为什么 System.Web.Mvc.HtmlHelper 似乎只是使用路由表中的第一个非忽略路由来生成链接等,但我找到了“BeginForm”问题的解决方法。
如果您在 Global.asax.cs 中命名了“默认”路由,例如:
那么您可以使用 Html.BeginFormRoute 方法并调用“默认”MVC 路由的名称,然后具体命名控制器和操作,结果在正确的网址:
HTH
I have experienced this exact problem. I'm not sure exactly why the System.Web.Mvc.HtmlHelper seems to just use the first non-ignore route in the routetable to generate links etc from but I have found a workaround for the "BeginForm" issue.
If you have named your "Default" route in the Global.asax.cs, for example:
Then you can use the Html.BeginFormRoute method and call the name of the "Default" MVC route, then name the controller and action specifically, resulting in the correct url:
HTH
试试这个
我想它应该可以解决你的问题。
Try this
I think it should solve your problem.
在默认路由之前添加此内容
Add this before default route