Home > DeveloperSection > Forums > Passing arguments to Url.Action
Goti Bandu

Total Post:119

Points:835
Posted on    November-27-2014 12:26 AM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 1 Reply(s)
 618  View(s)
Rate this:

What is the difference between these two ways of passing arguments to an Action?

 

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

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



Pravesh Singh

Total Post:411

Points:2881
Posted on    November-27-2014 1:49 AM

The difference is whether you're taking ASP.Net Routing into account.

Assuming you have a route defined as follows:

routes.MapRoute(

        name: "CallMyAction",

        url: "CallMyAction/{arg1}-{arg2}",

        defaults: new { controller = "MyController", action = "MyAction" });

Your 1st call would generate the following URL:

 

/CallMyAction?arg1=5&arg2=hello

While the 2nd call would generate a URL that is adhering the route pattern you defined:

 

/CallMyAction/5-hello


Don't want to miss updates? Please click the below button!

Follow MindStick