2012-12-15 83 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