views:

86

answers:

3

I need to create a custom routing for this url: /par1/par2/par3/par99/11

The url must redirect to home/index/11 11 is the Id,i need only this parameter, the other parameter (par1/par2/...) are only for SEO purpose and could be any word.

par1,par2,etc.. are created dinamically , so the Url could be: /par1/par2/11 or /par1/par2/par3/111 or /par1/3

etc...

thank you

A: 

Maybe you'll start with this and adjust to your needs:

routes.MapRoute(
    "Def0", // Route name
    "{controller}/{action}/{seo1}/{seo2}/{seo3}/{id}"
);

routes.MapRoute(
    "Def1", // Route name
    "{controller}/{action}/{seo1}/{seo2}/{id}"
);

routes.MapRoute(
    "Def2", // Route name
    "{controller}/{action}/{seo1}/{id}"
);

routes.MapRoute(
    "Def3", // Route name
    "{controller}/{action}/{id}"
);
LukLed
+1  A: 

you could just turn it around and go with

routes.MapRoute(
    "Route",
    "{id}/{*seostuff}",
    new {controller = "Home", action="Index", seo = UrlParameter.Optional});

that will allow you to map urls such as http://www.somesite.com/11/whatever/goes-here/will-be-whatever-you/want

BuildStarted
thank you.If i would create an url like this:http://www.somesite.com/category/seo1/seo2/SomeSeoWords/11/It would be simple?Note that there is the keyword Category at the begin, then there are some categories (for SEO) and then the Id.
Andrea
@Andrea: I wrote you an answer. What is wrong with it? This solution, which you accepted, doesn't support placing seo stuff before id.
LukLed
No, it only works the way I posted above because of the {*seostuff} is a catch-all and must be at the end. To fit with your example the second parameter should be `"category/{id}/{*seo}"` But it won't work if you want the id portion at the end.
BuildStarted
A: 

Since your id is at the end, you would need to make 99 routes to handle all the stuff in between in order for the id route to the controller (easily).

I would stick the id to the left of the "SEO stuff" if possible, so it can be thrown away easier, exactly like @Morder described. The {*seostuff} parameter catches forward slashes ('/'), whereas thee {seo1}, {seo2} parameters do not.

look at the way stackoverflow does urls; after the id everything is thrown away.

Mitch R.