mvc如何更改默认路由

iro*_*man 1 c# asp.net-mvc asp.net-mvc-routing

我正在阅读 Pro Asp.net mvc3 框架书。我想更改默认路由,以便我可以拥有不同的主页。我添加了一个名为 Pages 的新控制器和一个名为 Home 的视图。这就是我想要的主页。

我试过将此添加到我的 global.asax.cs

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

这会更改默认页面,但会破坏类别

  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}");
    }
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能使这项工作?

更新:

URLS: 主页转到项目列表

http://localhost/SportsStore/ 
Run Code Online (Sandbox Code Playgroud)

点击的类别

http://localhost/SportsStore/Chess?contoller=Product 
Run Code Online (Sandbox Code Playgroud)

为主页点击的控制器

 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);
        }
Run Code Online (Sandbox Code Playgroud)

我想在主页上点击的控制器

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

}
Run Code Online (Sandbox Code Playgroud)

谢谢,

iro*_*man 5

我能够让它像这样工作:

    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" });
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?