mvc how to change default route

后端 未结 3 440
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 14:06

I\'m going through the Pro Asp.net mvc3 framework book. I wanting to change the default route so that I can have a different home page. I\'ve added a new controller called P

相关标签:
3条回答
  • 2021-01-16 14:48

    First option is to use this way of page routing:

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

    This way your routes will be more REST way.

    Other way - use regex routes Defining routes using regular expressions

    PS: Cant check if this link is online now. Was ok couple of days ago.
    PS2: And Faust is right about route order. Greedy routes go last.
    PS3: Can you write URL scheme you want to implement?

    0 讨论(0)
  • 2021-01-16 14:51

    Be sure to place the default route at the very end of the route mappings. If it's last there's no way it can screw up the categories route.

    Update If this route:

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

    comes before the default, it will catch anything that you would expect your default route to catch except items with a third URL parameter (id).

    So for example:

    /somepage/home
    

    would get caught by this above route, rather than your default.

    So you probably want to delete this route.

    0 讨论(0)
  • 2021-01-16 14:53

    I was able to get it to work like this:

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

    is there a better way?

    0 讨论(0)
提交回复
热议问题