【问题标题】:mvc how to change default routemvc如何更改默认路由
【发布时间】:2012-08-07 21:11:54
【问题描述】:

我正在阅读 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}");
    }

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

更新:

网址: 主页转到项目列表

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

}

谢谢,

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    我能够让它像这样工作:

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

    有没有更好的方法?

    【讨论】:

    • +1 你能给出这条路线的 URL 示例吗routes.MapRoute(null, "", new {controller = "Pages", action = "Home"});
    【解决方案2】:

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

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

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

    其他方式 - 使用正则表达式路由Defining routes using regular expressions

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

    【讨论】:

      【解决方案3】:

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

      更新 如果这条路线:

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

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

      例如:

      /somepage/home
      

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

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

      【讨论】:

      • 我需要做些什么来让 mvc 知道我希望这是默认设置吗?
      • 我进行了您在更新中建议的更改,但这也不起作用。它仍然默认为另一个而不是我的新控制器
      • @ironman99:现在很难知道你是如何设置路线的。您能否 1) 使用当前完整的路由映射更新您的问题,以及 2) 准确指定哪些 URL 正在/未路由到您想要的控制器,以及您希望它路由到哪个控制器(如果有)?像这样:/some/url --> some-controller + some-action(或一些类似的明确表述)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 2012-03-17
      • 2010-11-01
      相关资源
      最近更新 更多