C# mvc如何更改默认路由

C# mvc如何更改默认路由,c#,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc Routing,我正在阅读Pro Asp.net mvc3框架手册。我想更改默认路线,这样我就可以有一个不同的主页。我添加了一个名为Pages的新控制器和一个名为Home的视图。这是我想要的作为我的主页 http://localhost/SportsStore/Chess?contoller=Product 我已尝试将此添加到我的global.asax.cs routes.MapRoute("MyRoute", "{controller}/{action}/{id}", ne

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

http://localhost/SportsStore/Chess?contoller=Product 
我已尝试将此添加到我的global.asax.cs

routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
                new { controller = "Pages", action = "Home", id = "DefautId" });
http://localhost/SportsStore/Chess?contoller=Product 
这会更改默认页面,但会搞乱类别

  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/Chess?contoller=Product 
我该怎么做才能让它工作

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

http://localhost/SportsStore/Chess?contoller=Product 
网址: 主页转到项目列表

http://localhost/SportsStore/Chess?contoller=Product 
点击类别

http://localhost/SportsStore/Chess?contoller=Product 
主页点击的控制器

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

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

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

}

谢谢,

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

http://localhost/SportsStore/Chess?contoller=Product 
更新 如果这条路线:

http://localhost/SportsStore/Chess?contoller=Product 
routes.MapRoute(null, "{controller}/{action}");
在默认值之前,它将捕获您期望默认路由捕获的任何内容,但带有第三个URL参数(id)的项目除外

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

http://localhost/SportsStore/Chess?contoller=Product 
/somepage/home
将被上述路径捕获,而不是默认路径

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

因此,您可能希望删除此路由。

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

http://localhost/SportsStore/Chess?contoller=Product 
更新 如果这条路线:

http://localhost/SportsStore/Chess?contoller=Product 
routes.MapRoute(null, "{controller}/{action}");
在默认值之前,它将捕获您期望默认路由捕获的任何内容,但带有第三个URL参数(id)的项目除外

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

http://localhost/SportsStore/Chess?contoller=Product 
/somepage/home
将被上述路径捕获,而不是默认路径

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

因此,您可能希望删除此路由。

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

http://localhost/SportsStore/Chess?contoller=Product 
routes.MapRoute("Give route a name btw","Page/{page}", // Matches /Page/2
   new {controller = "Product", action = "List", category = urlParameter.Optional},
   new {page = @"\d+"} 
);
这样你的路线会更方便休息

http://localhost/SportsStore/Chess?contoller=Product 
其他方式-使用正则表达式路由

http://localhost/SportsStore/Chess?contoller=Product 
PS:现在无法检查此链接是否在线。几天前还可以。
PS2:浮士德关于路线顺序的说法是对的。贪婪的路线走在最后。

PS3:你能写下你想要实现的URL方案吗

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

http://localhost/SportsStore/Chess?contoller=Product 
routes.MapRoute("Give route a name btw","Page/{page}", // Matches /Page/2
   new {controller = "Product", action = "List", category = urlParameter.Optional},
   new {page = @"\d+"} 
);
这样你的路线会更方便休息

http://localhost/SportsStore/Chess?contoller=Product 
其他方式-使用正则表达式路由

http://localhost/SportsStore/Chess?contoller=Product 
PS:现在无法检查此链接是否在线。几天前还可以。
PS2:浮士德关于路线顺序的说法是对的。贪婪的路线走在最后。

PS3:你能写下你想要实现的URL方案吗

我能让它像这样工作:

http://localhost/SportsStore/Chess?contoller=Product 
    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" });

有更好的方法吗?

我能让它像这样工作:

http://localhost/SportsStore/Chess?contoller=Product 
    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" });

有更好的方法吗?

我需要做些什么来让mvc知道我希望这是默认值吗?我做了您在更新中建议的更改,但也没用。它仍然默认为另一个,而不是我的新controller@ironman99当前位置现在很难知道路线是如何设置的。您能否1)使用当前完整的路由映射更新您的问题,以及2)准确指定哪些URL正在/没有路由到您想要的控制器,以及您想要路由到哪个控制器(如果有)?像这样:
/some/url-->某个控制器+某个动作
(或类似的明确表述)我需要做些什么来让mvc知道我希望这是默认值?我做了您在更新中建议的更改,但也没有用。它仍然默认为另一个,而不是我的新controller@ironman99当前位置现在很难知道路线是如何设置的。您能否1)使用当前完整的路由映射更新您的问题,以及2)准确指定哪些URL正在/没有路由到您想要的控制器,以及您想要路由到哪个控制器(如果有)?如下所示:
/some/url-->某个控制器+某个操作
(或类似的显式公式)+1您能给出此路由的url示例吗
routes.MapRoute(null,“,new{controller=“Pages”,action=“Home”})+1您能否给出此路由的Url示例
routes.MapRoute(null,“,new{controller=“Pages”,action=“Home”})
http://localhost/SportsStore/Chess?contoller=Product