0

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 dynamically , so the Url could be:

/par1/par2/11

or

/par1/par2/par3/111

or

/par1/3

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thank you. If i would create an url like this: 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: I wrote you an answer. What is wrong with it? This solution, which you accepted, doesn't support placing seo stuff before id.
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.
0

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

Comments

0

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.

Comments

0

I know this has already been accepted but I've written a route class that allows greedy segment to be anywhere in the URL definition.

In this case URL definition would be

{*seo}/{id}

Read all details about the custom route class and use it if you like:

Custom Asp.net MVC route class with catch-all segment anywhere in the URL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.