2014-05-13 218 views
1

將參數傳遞給Action的這兩種方式之間有什麼區別?將參數傳遞給Url.Action

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

第一個你沒有實際傳遞任何參數給Url.Action,只是連接兩個字符串 – CodingIntrigue

回答

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