1

So I have the basic Hello World service running in DNN 6.2 Services Framework, which is based on MVC. The route, taken from example, is:

routeManager.MapRoute("MyServices", "{controller}/{action}", new[] { "HelloWorldServices" });

When my action go to ~/DesktopModules/MyServices/API/Welcome/HelloWorld, it calls the HelloWorld function and works. So far, I've managed to figure out that I can do this:

routeManager.MapRoute("MyServices", "{controller}/{action}/{userID}", new[] { "HelloWorldServices" });

When I go to ~/DesktopModules/MyServices/API/Welcome/users, it calls the users function, which has an optional parameter for the ID, and that is working. But now I want to make an endpoint ~/DesktopModules/MyServices/API/Welcome/locations, which could take a location ID. I can't just write this:

routeManager.MapRoute("MyServices", "{controller}/{action}/{locationID}", new[] { "HelloWorldServices" });

or even

routeManager.MapRoute("MyServices", "{controller}/locations/{locationID}", new[] { "HelloWorldServices" });

It doesn't work that way. Whichever is first (users or locations) recognizes the ID and whichever is second does not. While I can figure out what does not work, I have no idea what will work, and have not been able to find out despite a lot of searching. I also found some examples that look like this:

routeManager.MapRoute("Html", "default", "", new { controller = "Service", action = "GetTabs" }, new[] { "DotNetNuke.Modules.Html" });

But I can't figure out how that works, either. Eventually I'll want to do "~/DesktopModules/MyServices/API/Welcome/users/userid/locations/" as well. If anyone could give me some examples, that would be much appreciated!

UPDATE: Based on the answer I looked up MVC routing. I realized I had to write it generically, and now have these working:

{controller}/{action}
{controller}/{action}/{id}
{controller}/{action}/{id}/{secondaction}
{controller}/{action}/{id}/{secondaction}/{secondid}

"id", "secondaction", and "secondid" are all optional string parameters. So I can have "users/userid/locations/locationid"... the "locations/locationid" part is handled in the "users" function.

MysteriousWhisper
  • 678
  • 1
  • 9
  • 19

1 Answers1

0

My general recommendation here would be to use a route map that is something like

{controller}/{action}/{id}

This is a typical default ASP.NET MVC route style. Then you could have a Users Method that as an optional id parameter, and a locations method with an optional id parameter, etc.

This will give you a truly dynamic route, and is a LOT easier to manage

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • Thanks -- I gave it a try, and at first, only one would work (either users or locations). But I realized that in my functions, my optional parameters were still named "userID" and "locationID;" they both worked when I made them both "id." However, I'm still trying to figure out how to do something like "users/(userid)/locations" – MysteriousWhisper Mar 29 '13 at 19:32