开发者

asp.net mvc2 Routes

开发者 https://www.devze.com 2023-01-17 16:27 出处:网络
So I have added this route to my map: routes.MapRoute( \"Default\", \"/Bikes/{id}\", new { controller = \"Bike\", action = \"Detail\" }

So I have added this route to my map:

    routes.MapRoute(
        "Default", 
        "/Bikes/{id}", 
        new { controller = "Bike", action = "Detail" }
    );

But for SEO reasons we need to have urls like: /bikes/54/name-of-bike/kind-of-bike/number-of-wheels ... etc etc. But everything after id (54) can be ignored.

Does anyone know how to create suc开发者_StackOverflow社区h a MapRoute to allow that, the route above does not work actually for urls that contain stuff after the id.


You can use a catchall parameter like so

routes.MapRoute(
    "Default", 
    "/Bikes/{id}/{*stuff}", 
    new { controller = "Bike", action = "Detail", stuff = UrlParameter.Optional }
);

then anything after id will be stored in stuff


Something like this

routes.MapRoute(
    "Default", 
    "/Bikes/{id}/{slug*}", 
    new { controller = "Bike", action = "Detail", 
          slug = UrlParameter.Optional}
);
0

精彩评论

暂无评论...
验证码 换一张
取 消