Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 388
Darcy Avatar asked Nov 29 '25 23:11

Darcy


1 Answers

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('/').

like image 96
SLaks Avatar answered Dec 02 '25 12:12

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!