2011-10-10 46 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 
}