Implementing Routes for Dynamic Actions in MVC 4.0

Is it possible to define a route in MVC that dynamically resolves an action based on a portion of the route?

public class PersonalController
{
  public ActionResult FriendGroup()
  {
      //code
  }

  public ActionResult RelativeGroup()
  {
      //code
  }

  public ActionResult GirlFriendGroup()
  {
      //code
  }
}

      

I want to implement routing for my group action method below

Url: www.ParlaGroups.com/FriendGroup
     www.ParlaGroups.com/RelativeGroup
     www.ParlaGroups.com/GirlFriendGroup

routes.MapRoute(
    "Friends",
    "/Personal/{Friend}Group",
    new { controller = "Personal", action = "{Friend}Group" }
);
routes.MapRoute(
    "Friends",
    "/Personal/{Relative}Group",
    new { controller = "Personal", action = "{Relative}Group" }
);
routes.MapRoute(
    "Friends",
    "/Personal/{GirlFriend}Group",
    new { controller = "Personal", action = "{GirlFriend}Group" }
);

      

How can I accomplish the above routing implementation?

+3


source to share


2 answers


public class PersonalController
{
  public ActionResult FriendGroup()
  {
   //code
  }

  public ActionResult RelativeGroup()
  {
   //code
  }

  public ActionResult GirlFriendGroup()
  {
   //code
  }
}

 Url: www.ParlaGroups.com/FriendGroup
      www.ParlaGroups.com/RelativeGroup
      www.ParlaGroups.com/GirlFriendGroup


 routes.MapRoute(
  "Friends",
  "/{action}",
  new { controller = "Personal"}
 );

      



0


source


The following route will allow MVC to define ActionResult using the second part of the Url:

routes.MapRoute(
"Friends",
"Personal/{action}",
new { controller = "Personal" }
);

      



The following urls will match:

www.ParlaGroups.com/Personal/FriendGroup Where "FriendGroup" is ActionResult www.ParlaGroups.com/Personal/RelativeGroup Where "RelativeGroup" is ActionResult www.ParlaGroups.com/Personal/GirlFriendGroup Where "GirlFriendGroup" is ActionResult

+4


source







All Articles