asp.net-mvc - Html.BeginForm 的 ASP.Net MVC 路由问题

标签 asp.net-mvc routing html.beginform

使用 MVC,我有一个 html 表单助手:

using (Html.BeginForm("ActionOne", "ControllerOne")) ...

使用默认路由,action属性的输出如预期:

<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&amp;controller=ControllerOne"

这是设计使然还是我遗漏了一些基本的东西?

干杯!

最佳答案

我遇到过这个确切的问题。我不确定为什么 System.Web.Mvc.HtmlHelper 似乎只是使用路由表中的第一个非忽略路由来生成链接等,但我找到了解决“BeginForm”问题的方法。

如果您在 Global.asax.cs 中命名了“默认”路由,例如:

routes.MapRoute("Default", "{controller}/{action}", new {controller = "Home", action = "Index" });

然后你可以使用 Html.BeginFormRoute 方法并调用“默认”MVC 路由的名称,然后具体命名 Controller 和操作,从而得到正确的 url:

using (Html.BeginRouteForm("Default", new { controller="YourController", action = "YourFormAction" })) { }

HTH

关于asp.net-mvc - Html.BeginForm 的 ASP.Net MVC 路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1754771/

相关文章:

asp.net-mvc - 将 Html.BeginForm 与查询字符串一起使用

asp.net-mvc-3 - mvc3 : html. beginform 搜索返回空查询字符串

.net - 在 beginform 中使用链接而不是按钮

ruby-on-rails - Ruby on Rails 与 ASP.NET MVC 3 - 哪个开发网站更快?

asp.net-mvc - MVC 中的 ASP.NET Identity IUserEmailStore 错误

email - 使用 mailgun 进行邮件路由

Angular 2/4。重用策略。如何通知组件它已被重用?

javascript - 将 Typeahead 与 SignalR 结合使用

c# - 如何为路由实现另一个 var 参数名称

ruby-on-rails - ActionController::RoutingError(未初始化的< Controller 名称>)