在MVC 4.0实现动态路由的行动 [英] Implementing Routes for dynamic actions in MVC 4.0

查看:543
本文介绍了在MVC 4.0实现动态路由的行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能定义一个MVC路由动态解析基于该路线的一部分行动?

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

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

  public ActionResult RelativeGroup()
  {
      //code
  }

  public ActionResult GirlFriendGroup()
  {
      //code
  }
}

我要实现我的行动组在下面的方法

I want to implement a 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 do the above routing implementation?

推荐答案

下面的路由将允许MVC使用URL的第二部分来确定的ActionResult:

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

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

以下URL将匹配:​​

The following urls will match:

www.ParlaGroups.com/Personal/FriendGroup的其中FriendGroup是的ActionResult
www.ParlaGroups.com/Personal/RelativeGroup的其中RelativeGroup是的ActionResult
www.ParlaGroups.com/Personal/GirlFriendGroup的其中GirlFriendGroup是的ActionResult

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

这篇关于在MVC 4.0实现动态路由的行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆