3

是否可以在MVC中定義一個根據部分路徑動態解析動作的路由?在MVC 4.0中實現動態行爲的路線

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", 
    "/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" } 
); 

我該怎麼辦上述路由實現?

+0

這是路由101討好文檔:http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp- net-mvc-routing-overview-cs – 2014-09-10 13:08:21

回答

0
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"} 
); 
4

下列路線將允許MVC通過使用URL的第二部分以確定所述的ActionResult:

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

以下URL將匹配:

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

+0

對不起,我只是用更多的細節更新了我的問題 – 2014-09-10 12:28:16

+0

好吧,我不再清楚你想要實現什麼。你可以從你的行爲結果方法的名字中刪除「Group」,並且有一個路由,如:routes.MapRoute( 「Friends」, 「Personal/{action} Group」, new {controller =「Personal」}} );' – Carl 2014-09-10 12:52:12