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?

2 Answers 2

14

You can make a wildcard route:

MapRoute("{controller}/{action}/{*id}")
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)
{

}

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.