将参数传递给Url.Action

时间:2014-05-13 11:57:41

标签: asp.net asp.net-mvc asp.net-routing

这两种方法将参数传递给Action有什么区别?

(1) @Url.Action("MyAction", "MyController")?arg1=5&arg2="hello";
(2) @Url.Action("MyAction", "MyController", new {arg1=5, arg2="hello"});

1 个答案:

答案 0 :(得分:1)

区别在于您是否考虑了ASP.Net 路由

假设您的路线定义如下:

routes.MapRoute(
        name: "CallMyAction",
        url: "CallMyAction/{arg1}-{arg2}",
        defaults: new { controller = "MyController", action = "MyAction" });

您的第一个电话将生成以下网址:

/CallMyAction?arg1=5&arg2=hello

第二次通话时会生成一个符合您定义的路线模式的网址:

/CallMyAction/5-hello