7

In a URL like http://mysite.com/controller/action/123/name-of-article, is there a way to define the route so that the '123/name-of-article' part is passed in as a string?

nneonneo
181k37 gold badges331 silver badges412 bronze badges
asked Jun 26, 2011 at 23:11

2 Answers 2

14

You can make a wildcard route:

MapRoute("{controller}/{action}/{*id}")
answered Jun 26, 2011 at 23:18
Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure what your requirements are, but are you sure you want to pass the name of the article to the controller? Would you be better off just passing the ID then doing a SELECT from the database to get the Name?

If you still wanted to have a URL like http://mysite.com/controller/action/123/name-of-article but only pass in the ID you can use a route like this

 routes.MapRoute(
 "MyNewRoute", // Route name
 "articles/edit/{id}/{name}", // URL
 new { controller = "Articles", action = "Edit", id = "" }, // Defaults
 new[] { "YourApp.UI.Controllers" } // Namespaces
 );

Then your controller action

public ActionResult Edit(int id)
{
}
nneonneo
181k37 gold badges331 silver badges412 bronze badges
answered Jun 27, 2011 at 0:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.