1

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

我已经尝试将此添加到我的 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}");
    }

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

更新:

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

3 回答 3

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" });

有没有更好的办法?

于 2012-08-10T18:34:17.560 回答
1

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

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

这样,您的路线将更加 REST 方式。

其他方式 - 使用正则表达式路由使用正则表达式定义路由

PS:无法查看此链接现在是否在线。前几天还好。
PS2:浮士德关于路线顺序是正确的。贪婪的路线排在最后。
PS3:你能写出你想实现的URL scheme吗?

于 2012-08-07T21:28:35.803 回答
0

确保将默认路由放在路由映射的最后。如果它是最后一个,它就不可能搞砸类别路线。

更新 如果这条路线:

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

出现在默认值之前,它将捕获您希望默认路由捕获的任何内容,但具有第三个 URL 参数 (id) 的项目除外。

例如:

/somepage/home

会被上面的这条路线抓住,而不是你的默认路线。

所以你可能想删除这条路线。

于 2012-08-07T21:26:18.137 回答