views:

97

answers:

1

I am attempting to route a URL that does not have a static action i.e. Users can create systems which can be represented by any string. I would like to have a URL like the following:

http://yousite.com/System/WIN1234/Configure

By default the routing mechanism thinks that WIN1234 is the action, whereas I would like to be able to catch WIN1234 and make a decision on which method to throw. As in:

public void RouteSystemRequest(string system, string action)
{

  switch (action)
  {

    case "Configure":

        ConfigureSystem(string system);
        break;

  }

}

How can I accomplish this? Is this logical or am I thinking about this all wrong?

Thanks! George

A: 

What you need to do is possible. You need to set up a default route that points to the System Action, and you need to accept the other values as parameters to the Action.

    //General
    routes.MapRoute(
        "Default7",                                              // Route name
        "{action}/{param1}/{param2}",                           // URL with parameters
        new { controller = "Home", action = "Index", param1 = "", param2 = "" }  // Parameter defaults
    );

In your code you will then get the values Win1234 and Configure as default.

You can then implement your switching logic and use RedirectToAction to move to the action you desire.

Cyril Gupta
I set up the code that you posted, changing the controller from Home to Systems. I would expect the route to go toSystemsController.Index(string id, string system)id = param1, system = param2I get a resource cannot be found error. What am I doing wrong?
George
This route needs to be the first in your sequence. Also the controller you set up must have arguments named param1 or param2. not system and id.
Cyril Gupta
I would like the overall routing structure to remain the same with the exception of the Systems controller. Is there a way to put a constraint on the route to say, only route to the RouteSystemRequest(string systemName, string action) when the controller is System. i.e.Account/Setup will still follow the default controller = "Account" and action = "Setup", but System/WIN1234/Configure will route to RouteToSystem(string systemName, string action) Thanks!George
George
Yes, it's indeed possible to set up a route that send the request to any controller/action. You can have multiple insertions in the routing code (one entry for each route), so you just need to create another Maproute entry for your System route. What is the problem in setting it up?(If you are finding my answers helpful, I'd appreciate a vote)
Cyril Gupta