2011-10-10 116 views
0

目前我有一个名为StoreController的控制器。有三类:书籍,电影和游戏。我怎样才能确保网址的ASP.NET MVC路由:动作方法的动态名称

  • http://mywebsite.com/store/books
  • http://mywebsite.com/store/movies
  • HTTP: //mywebsite.com/store/games

匹配单个动作方法。现在,我有三个独立的行动方法books(); movies(); games();做同样的事情,即列出其中的产品

回答

3

你试过这样吗?

routes.MapRoute(
       "Default", // Route name 
       "{controller}/{id}", // URL with parameters 
       new { controller = "Store", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
       , null } 
       ) 

,你让控制器像

public ActionResult Index(string id) 
{ 
    if(id == "books"){ 


    } 
    else if(id == "movies"){ 

    } 
    else{// this is null case 


    } 

    return Content("hello");// test 
}