开发者

MVC Advanced Routing

开发者 https://www.devze.com 2022-12-13 12:11 出处:网络
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:

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消