It seems like it should be a possible to the names and values of each parameter without this kind of tedious code.

Sometimes you can get the parameters which the action method was called with, by looking in RouteData, but this isn't always the case, particularly if the action method was invoked with an ajax request, the parameters may not show up in the RouteData, and instead show up in the Request Params.

What I'm looking for, is a generic way to get each parameter that is defined in the action method signature at run-time, and gets the parameter's actual object, run-time value, not just a string. Further more, it should work no matter how the action method was invoked, whether it may be the result of ChildActionExtensions.Action or an ajax callback.

this interests me, however, I am also waiting for someone else to say you are doing it wrong. I would be interested in more code if possible.
–
Valamas - AUSNov 9 '11 at 4:59

@Valamas so am I. It's kind of surprising that now at the 3rd release of the framework, this question hasn't been asked yet. What won't be that surprising, is if someone can't really answer this question so their answer becomes something like "why do you need to...?"
–
InquisitorNov 10 '11 at 0:36

1 Answer
1

Your code sample is setting values back into the RouteData collection. Are you trying to pass parameters to your view using the RouteData collection? That's not what it exists for, you might consider using ViewBag instead.

Or, create a POCO which contains all your properties and let the data binder do all the work (so use @model YourType in your view and pass a single argument to your view. The default model binder will map the individual argument values for you).

As far as the input value collections are concerned, there's a good reason why the value is not to be found consistently in the collections you've mentioned.

Perhaps the trick here is to clarify what's going on prior to your action being invoked. The arguments to your action method can come from more than one source. For example, it may come from:

The URL Path

The URL query string (eg: in a GET, the parameters after the question mark ?)

POSTed form data

Explicit arguments from another action

In your code sample above, the RouteData collection will only contain the value of "x" if your route has a matching parameter name.

For example: "{controller}/{action}/{x}". (this is a custom route pulling "x" from the path)

Failing that, the values will be resolved using the default model binder and will be pulled from either the query string parameters or POST data as the case may be.

The route value will take precedence. So if the above custom route was applied, the following URL:

http://www.example.com/Something/Index/1?x=2

would invoke your action with x=1. The 1 would then be found in the RouteData as pulled from the URL path and the x=2 found in the Request.QueryString would be ignored.

If you are concerned with how x got its value, then you must take into account all of the above so you know where to look. There is also the question of which route is applied to the request, but that's another topic altogether.

All the input came across the wire as text.. it was the model binder that examined your action signature and converted to the types you specified (wherever that is possible).

So, I don't think what you are asking for exists even conceptually in this setting.