mvc如何更改默认路由

mvc如何更改默认路由

问题描述:

我正在通过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(); 
    } 

} 

感谢,

我能得到它像这样工作:

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

确保将默认路由在很路线映射的结尾。如果它是最后一个,那么它就不可能搞砸分类路线。

更新 如果这条航线:

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

第一种选择是使用页面路由这样:

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方案吗?