2012-08-07 192 views
0

我正在通過Pro Asp.net mvc3框架書。我想要更改默認路由,以便我可以擁有不同的主頁。我添加了一個名爲Pages的新控制器和一個名爲Home的視圖。這就是我想要的主頁。mvc如何更改默認路由

我試着加入這個對我的global.asax.cs

routes.MapRoute("MyRoute", "{controller}/{action}/{id}", 
       new { controller = "Pages", action = "Home", id = "DefautId" }); 

這改變了默認頁面,但它搞砸了的類別

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 


     routes.MapRoute(null, 
         "", // Only matches the empty URL (i.e. /) 
         new 
          { 
           controller = "Product", 
           action = "List", 
           category = (string) null, 
           page = 1 
          } 
      ); 

     routes.MapRoute(null, 
         "Page{page}", // Matches /Page2, /Page123, but not /PageXYZ 
         new {controller = "Product", action = "List", category = (string) null}, 
         new {page = @"\d+"} // Constraints: page must be numerical 
      ); 

     routes.MapRoute(null, 
         "{category}", // Matches /Football or /AnythingWithNoSlash 
         new {controller = "Product", action = "List", page = 1} 
      ); 

     routes.MapRoute(null, 
         "{category}/Page{page}", // Matches /Football/Page567 
         new {controller = "Product", action = "List"}, // Defaults 
         new {page = @"\d+"} // Constraints: page must be numerical 
      ); 

     routes.MapRoute(null, "{controller}/{action}"); 
    } 

我應該怎麼做,使這個工作?

UPDATE:

URLS: 主頁進入列表項的

http://localhost/SportsStore/ 

點擊類別

http://localhost/SportsStore/Chess?contoller=Product 

控制器,被擊中在主頁

public class ProductController : Controller 
    { 
     private readonly IProductRepository repository; 
     public int PageSize = 4; 

     public ProductController(IProductRepository repoParam) 
     { 
      repository = repoParam; 
     } 


     public ViewResult List(string category, int page = 1) 
     { 
      var viewModel = new ProductsListViewModel 
           { 
            Products = repository.Products 
             .Where(p => category == null || p.Category == category) 
             .OrderBy(p => p.ProductID) 
             .Skip((page - 1)*PageSize) 
             .Take(PageSize), 
            PagingInfo = new PagingInfo 
                { 
                 CurrentPage = page, 
                 ItemsPerPage = PageSize, 
                 TotalItems = category == null 
                      ? repository.Products.Count() 
                      : repository.Products.Where(
                       e => e.Category == category).Count() 
                }, 
            CurrentCategory = category 
           }; 

      return View(viewModel); 
     } 

控制器,我想被擊中的主頁

public class PagesController : Controller 
{ 
    public ViewResult Home() 
    { 
     return View(); 
    } 

} 

感謝,

回答

4

我能得到它像這樣工作:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(null, "", new {controller = "Pages", action = "Home"}); 

    //routes.MapRoute(null, 
    //    "", // Only matches the empty URL (i.e. /) 
    //    new 
    //     { 
    //      controller = "Product", 
    //      action = "List", 
    //      category = (string)null, 
    //      page = 1 
    //     } 
    // ); 

    routes.MapRoute(null, 
        "Page{page}", // Matches /Page2, /Page123, but not /PageXYZ 
        new {controller = "Product", action = "List", category = (string) null}, 
        new {page = @"\d+"} // Constraints: page must be numerical 
     ); 

    routes.MapRoute(null, 
        "{category}", // Matches /Football or /AnythingWithNoSlash 
        new {controller = "Product", action = "List", page = 1} 
     ); 

    routes.MapRoute(null, 
        "{category}/Page{page}", // Matches /Football/Page567 
        new {controller = "Product", action = "List"}, // Defaults 
        new {page = @"\d+"} // Constraints: page must be numerical 
     ); 


    //routes.MapRoute(null, "{controller}/{action}"); 

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

    //routes.MapRoute("MyRoute", "{controller}/{action}", 
    // new { controller = "Pages", action = "Home" }); 

有沒有更好的辦法?

+0

+1你可以給這個Route路由的Url例子嗎? .MapRoute(null,「」,new {controller =「Pages」,action =「Home」});' – 2013-07-26 14:32:01

0

確保將默認路由在很路線映射的結尾。如果它是最後一個,那麼它就不可能搞砸分類路線。

更新 如果這條航線:

routes.MapRoute(null, "{controller}/{action}"); 

自帶的默認之前,它會抓住任何東西,你會期望你的默認路由趕上除了與第三URL參數(ID)項目。

因此,例如:

/somepage/home 

將通過這個途徑以上被逮住,而不是默認。

所以你可能要刪除這條路線。

+0

沒有工作。默認主頁沒有變化 – ironman 2012-08-07 21:34:49

+0

有什麼我需要做的,讓mvc知道我想這是默認的? – ironman 2012-08-07 21:53:18

+0

我在您的更新中提出了您建議的更改,但這也沒有奏效。它仍然是默認的,而不是我的新控制器 – ironman 2012-08-08 13:47:07

1

第一種選擇是使用頁面路由這樣:

routes.MapRoute("Give route a name btw","Page/{page}", // Matches /Page/2 
    new {controller = "Product", action = "List", category = urlParameter.Optional}, 
    new {page = @"\d+"} 
); 

這樣你的路線將是更多的休息方式。

其他方式 - 使用正則表達式的路線Defining routes using regular expressions

PS:廣東話檢查,如果這個鏈接是網上現在。前幾天還好。
PS2:Faust是關於路線順序的。貪婪的路線走到最後。
PS3:你可以寫你想要實現的URL方案嗎?