2010-08-30 95 views
0

改善網址在asp.net MVC2改善網址

目前,我有形式的鏈接(顯示產品信息):

http://localhost:XXXX/Products/?page=1

我要清理這個成以下形式:

http://localhost:XXXX/Products/Page1

我想我需要做到這一點routes.MapRoute,像這樣:

routes.MapRoute(null, "/Products/Page{page}", new {controller = "ProductController", action = "Index"}); 

這是默認路由上面放(所以應該覆蓋導致我相信)

產品控制器看起來是這樣的:

// 
    // GET: /Products/ 
    public ActionResult Index([DefaultValue(1)] int page) 
    { 
     var productsToShow = //omitted for simplicity 

     var viewModel = new ProductIndexViewModel 
          { 
           ProductList = //omitted for simplicity, 
           PagingInfo = new PagingInfo 
               { 
                CurrentPage = page, 
                ItemsPerPage = PageSize, 
                TotalItems = productsToShow.Count() 
               } 
          }; 

     //Passed to view as ViewData.Model (or simply Model) 
     return View(viewModel); 
    } 

什麼時我做錯了?

回答

2

變化routes.MapRoute

routes.MapRoute(null, "Products/Page{page}", new {controller = "Products", action = "Index"}); 
+1

正確 - 在*公約*的是,所有控制器類字'Controller'結束。因此,路線並不需要您將完整的單詞添加到路線詳情中,如上所述。所以就像@Akyegane所說的,controller =「Products」..而不是「ProductsController」 – 2010-08-30 04:59:36