简体   繁体   中英

Is it possible to have route multiple parameters in asp.net mvc using maproute

I want a user to be able to access objects (could be JSON or XML) using a restful syntax rather than having to use query strings.

So instead of http://mywebsite.com/objects/get=obj1&get=obj2&get=someotherobject/ they could do something like http://mywebsite.com/objects/obj1/obj2/ and the xml/JSON would be returned. They could list the objects in any order just like you can with query strings.

In asp.net mvc you map a route like so:

       routes.MapRoute(
           "MyRoute",
           "MyController/MyAction/{param}",
           new { controller = "MyController", action = "MyAction", param = "" }
       );

I would want to do something like:

       routes.MapRoute(
           "MyRoute",
           "MyController/MyAction/{params}",
           new { controller = "MyController", action = "MyAction", params = [] }
       );

where the params array would contain each get.

You could use a catchall parameter

   routes.MapRoute(
       "MyRoute",
       "MyController/MyAction/{*params}",
       new { controller = "MyController", action = "MyAction"}
   );

This would pass params as a string that you could split on a / to get an array.

Not quite.

You can create a wildcard parameter by mapping {*params} .
This will give you a single string containing all of the parameters, which you can then .Split('/') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM