Passing arguments to Url.Action

Total Post:119

Points:835
 1205  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. 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

      Modified On Apr-07-2018 01:12:15 AM

Answer

NEWSLETTER

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