2012-12-15 82 views
1

也許我的問題很容易應對,但我在MVC3是新的。地圖路由MVC3

我有我的MVC3 Web項目,這些可能的途徑:

  • /品牌/ brandID
  • /品牌/ {行動}/{參數}

其中品牌是我的控制器和brandID是接收我的索引操作的特定參數。另外,我的品牌控制器有更多的動作,我需要在Global.asax定義正確的地圖路線,使其工作。

+0

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs – Jared

+1

爲了傳遞品牌標識,您需要控制器操作。例如:'/品牌/索引/ 1' –

+0

是,指數在收到
參數brandID。我試圖定義這些地圖路線:routes.MapRoute( 「品牌」, 「品牌/ {行動}/{}參數」, 新{控制器= 「品牌」,行動= 「指數」,參數= UrlParameter .Optional} ); routes.MapRoute( 「Brands2」, 「brands/{* path}」, new {controller =「Brands」,action =「Index」} );但它爲你回答不工作... – jasalvador

回答

0

有一個在Custom Routing(修改你的情況下)的一個例子。

的Global.asax:

routes.MapRoute(
    "BrandDetails", 
    "Brands/{brandID}", 
    new { controller = "Brands", action = "Details" }, 
    new { brandID = @"\d+" } 
); 

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
); 

BrandsController:

public ActionResult Index() 
{ 
    return View(); 
} 

public ActionResult Details(int brandID) 
{ 
    return this.View(brandID); 
} 
+0

感謝。但是,好吧,我有品牌控制器的一些行動:即。 /品牌/ GetBrands /過濾器。而且,我有品牌/索引/ BrandID行動,其中BrandID是品牌(這是變量)的標識符。有了您的第一條路線(「品牌」)的詳細動作總是與我的問題 – jasalvador

+0

reciving的要求,即使是GetBrands請求......所以,還是我在我的答案更新的代碼。適用於我。 – alekseyk

+0

大,這裏唯一的一點是,brandID是一個字符串,但改變正則表達式,我認爲這是可行的。非常感謝你!! – jasalvador