10
votes

I'm looking for some information on Routing in MVC with C#. I'm currently very aware of the basics of routing in MVC, but what i'm looking for is somewhat difficult to find.

Effectively, what I want to find is a way of defining a single route that takes a single parameter.

The common examples I have found online is all based around the example

routes.MapRoute(
    "Default",
    "{controller}.mvc/{action}/{id}"
    new { controller = "Default", action="Index", id=""});

By mapping this route, you can map to any action in any controller, but if you want to pass anything into the action, the method parameter must be called "id". I want to find a way around this if it's possible, so that I don't have to constantly specify routes just to use a different parameter name in my actions.

Has anyone any ideas, or found a way around this?

5

5 Answers

7
votes

If you want to have a different parameter name and keep the same routing variable, use the FromUri attribute like so:

public ActionResult MyView([FromUri(Name = "id")] string parameterThatMapsToId)
{
   // do stuff
}

In your routes, all you need is:

routes.MapRoute(
  "Default",
  "{controller}.mvc/{action}/{id}"
  new { controller = "Default", action="Index", id=""});
5
votes

I don't think that you can do exactly what you are asking. When MVC invokes an action it looks for parameters in routes, request params and the query string. It's always looking to match the parameter name.

Perhaps good old query string will meet your needs.

~/mycontroller/myaction/?foobar=123

will pass 123 to this action:

public ActionResult MyAction(int? foobar)
3
votes

I know this is centuries ago, but hope it still helps someone. I asked the same question before. I think this is what you are looking for. An answer quoted from my question post: "The {*pathInfo} bit is called a slug. it's basically a wildcard saying "everything after this point is stuffed into a parameter called pathInfo". Thus if you have "{resource}.axd/{*pathInfo}" and a url like this: http://blah/foo.axd/foo/bar/baz/bing then two parameters get created, one called resource, which would contain foo and one called pathInfo which contains foo/bar/baz/bing."

0
votes

You can construct the routes as you like

routes.MapRoute(
    "Default",
    "{controller}.mvc/{action}/{param1}/{param2}/{param3}"
    new { controller = "Default", action="Index", param1="", param2="", param3=""});

Also, look at this post, it contains all kind of samples in the comments section

0
votes

Although you still can't use the FromUri attribute, you can however use the Route attribute, like so

[Route("~/Policy/PriorAddressDelete/{sequence}")]
public ActionResult PriorAddressDelete(int sequence)
{
  Policy.RemoveScheduledPriorAddressItem(sequence);
  return RedirectToAction("Information", new { id = Policy.Id });
}

Technically this adds a new route, but at least it doesn't clutter up your routeconfig. It puts the route definition right by where it's used, which I like (less hunting things down).

Remember, in order to use attribute routing, this must be in your routeconfig file above your defined routes:

routes.MapMvcAttributeRoutes();