views:

769

answers:

3

Hello there!

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?

+1  A: 

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

Eduardo Molteni
Kinda missing the point... he wants to basically have {controller}.mvc/{action}/{*} where star is any parameter name.
Timothy Khouri
Yea, thanks Timothy. That's correct. Maybe i didn't word the question very well.
Jimmeh
Not sure what you want, maybe you can try using *For Each value In requestContext.RouteData.Values* and them quering *value.Key* and *value.Value*
Eduardo Molteni
A: 

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)
Tim Scott
A: 

So to clarify, you want either a parameterless action or a subset of parameters, and then some kind of "property bag" of the other URL parameters? I haven't tried this, but what if your action took a "params" parameter? I don't know what MVC would do with something like that, but that's kind of what you want, it sounds like.

GalacticCowboy