PASSING ARGUMENTS TO URL.ACTION

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
 972  View(s)
Ratings:
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"});

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Passing arguments to Url.Action

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!