使用Html.BeginForm()与自定义路由

时间:2022-12-06 21:46:58

This is as you surely know the default route:

您肯定知道默认路由:

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

Let's say I use the BeginForm() method like this:

假设我使用了BeginForm()方法,如下所示:

@using (Html.BeginForm("MyAction", "MyController", new { id = 4 }))

This will render the following form tag:

这将呈现以下表单标记:

<form method="post" action="/MyController/MyAction/4">

Now, let's say I've made a custom route:

现在,假设我定制了一条路线:

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

When I create a form I'd like it to look like this:

当我创建一个表单时,我希望它看起来像这样:

<form method="post" action="/MyController/4/MyAction">

However, if I use BeginForm() as in the example above, I will get a url that matches the default route instead. Is there a way to tell BeginForm() to use my custom route instead of the default one when creating the url for the action? Or does BeginForm() always produce urls that follows the default route pattern?

但是,如果我像上面的示例那样使用BeginForm(),我将获得与默认路由匹配的url。是否有一种方法可以告诉BeginForm()在为操作创建url时使用我的自定义路由而不是默认路由?或者BeginForm()总是生成遵循默认路由模式的url吗?

I'm using asp.net mvc 3 if it matters.

如果它很重要,我将使用asp.net mvc 3。

3 个解决方案

#1


36  

You can use the Html.BeginRouteForm() method from the HtmlHelper class.

可以从HtmlHelper类中使用Html.BeginRouteForm()方法。

@Html.BeginRouteForm("MyCustomRoute", 
   new { controller = "MyController", action = "MyAction" })

#2


4  

The reason for that is that the routing system picks the first route that matches to build the route. So to circumvent this issue you could reorder your routes but that is quite fragile.

原因是路由系统选择与构建路由匹配的第一条路由。为了避免这个问题,你可以重新排序你的路线,但这是非常脆弱的。

Since the BeginForm method does not add much value you could use the the HTML form element to construct your form and build the action via a @Url.RouteUrl() somehow like this

由于BeginForm方法没有添加太多的值,所以您可以使用HTML表单元素来构造表单并通过@Url.RouteUrl()来构建操作。

@Url.RouteUrl("MyCustomRoute", new { controller = "MyController", action = "MyAction" })

Note that I'm using a named route here!

注意,我在这里使用的是命名路由!

#3


1  

If you already defined the route in your global.asax, you can use this syntax:

如果您已经在全局中定义了路径。asax,你可以使用这个语法:

global.asax:

global.asax:

routes.MapRoute("MyCustomRoute", "my-controller/{action}",
    new { controller = "MyController", action = "Index" });

controller (MyControllerController.cs):

控制器(MyControllerController.cs):

[ActionName("my-action")]
public ActionResult MyAction()
{
}

razor view (my-action.cshtml):

razor视图(my-action.cshtml):

@Html.BeginRouteForm("MyCustomRoute", new { action = "my-action" })
{
}

#1


36  

You can use the Html.BeginRouteForm() method from the HtmlHelper class.

可以从HtmlHelper类中使用Html.BeginRouteForm()方法。

@Html.BeginRouteForm("MyCustomRoute", 
   new { controller = "MyController", action = "MyAction" })

#2


4  

The reason for that is that the routing system picks the first route that matches to build the route. So to circumvent this issue you could reorder your routes but that is quite fragile.

原因是路由系统选择与构建路由匹配的第一条路由。为了避免这个问题,你可以重新排序你的路线,但这是非常脆弱的。

Since the BeginForm method does not add much value you could use the the HTML form element to construct your form and build the action via a @Url.RouteUrl() somehow like this

由于BeginForm方法没有添加太多的值,所以您可以使用HTML表单元素来构造表单并通过@Url.RouteUrl()来构建操作。

@Url.RouteUrl("MyCustomRoute", new { controller = "MyController", action = "MyAction" })

Note that I'm using a named route here!

注意,我在这里使用的是命名路由!

#3


1  

If you already defined the route in your global.asax, you can use this syntax:

如果您已经在全局中定义了路径。asax,你可以使用这个语法:

global.asax:

global.asax:

routes.MapRoute("MyCustomRoute", "my-controller/{action}",
    new { controller = "MyController", action = "Index" });

controller (MyControllerController.cs):

控制器(MyControllerController.cs):

[ActionName("my-action")]
public ActionResult MyAction()
{
}

razor view (my-action.cshtml):

razor视图(my-action.cshtml):

@Html.BeginRouteForm("MyCustomRoute", new { action = "my-action" })
{
}