Bernardmoes
Bernardmoes

Reputation: 340

How can i get an ID as parameter from an URL

I want to route the following URL;

/anything/anything-v43243-anything

How can i route this to a specific controller and action with that id as parameter? The text "anything" has to be a text with at least a few characters. The id needs to start with the letter "v". I want this to create friendly URL's

Upvotes: 4

Views: 116

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039348

You could write a custom route for that and appropriate constraints for the different parts:

routes.MapRoute(
    "myroute",
    "anything/{x}-{id}-{y}",
    new { controller = "SomeController", action = "SomeAction" },
    new { x = "[a-z]+", y = "[a-z]+", id = @"\d+" }
);

Upvotes: 1

Related Questions