Html.BeginForm 的 ASP.Net MVC 路由问题

发布于 2024-08-12 04:29:27 字数 734 浏览 4 评论 0原文

使用 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&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琉璃繁缕 2024-08-19 04:29:27

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

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

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

那么您可以使用 Html.BeginFormRoute 方法并调用“默认”MVC 路由的名称,然后具体命名控制器和操作,结果在正确的网址:

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

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:

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

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:

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

HTH

琉璃繁缕 2024-08-19 04:29:27

试试这个

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

        routes.Add("testRoute", new Route("MyUrl/***{action}/{controller}***", new MvcRouteHandler()));

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

我想它应该可以解决你的问题。

Try this

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

        routes.Add("testRoute", new Route("MyUrl/***{action}/{controller}***", new MvcRouteHandler()));

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

I think it should solve your problem.

野味少女 2024-08-19 04:29:27

在默认路由之前添加此内容

      routes.MapRoute("", "ControllerOne/ActionOne", new { controller = "ControllerOne", action = "ActionOneOne"});

Add this before default route

      routes.MapRoute("", "ControllerOne/ActionOne", new { controller = "ControllerOne", action = "ActionOneOne"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文