3

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.

Darcy
  • 5,228
  • 12
  • 53
  • 79

2 Answers2

3

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.

detaylor
  • 7,112
  • 1
  • 27
  • 46
3

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Is it possible to move the ASP.NET MVC MapRoutes to XML files as opposed to programmatically using routes.MapRoute? Also, would'nt it be better practice to push it to an XML file as opposed to coding it to C# and/or VB.NET? – crazyTech Apr 11 '13 at 18:17
  • @user1338998: You could write your own code that reads the XML and calls `MapRoute()`. Someone else may have written such code – SLaks Apr 11 '13 at 19:08
  • LOL, I don't want to start a vicious debate between .NET Developers and JEE developers. However, I would like to mention that in JEE technologies, configuration of mappings to Views in JSF MVC and Struts MVC would always be in an XML file like faces-config.xml and struts-config.xml which is the proper practice because we don't want to recompile. In other words, we can just modify the xml, and restart the server. – crazyTech Apr 11 '13 at 21:00