views:

334

answers:

1

Hi!

I was wondering whether there is a way to create an ActionLink or similar, that changes only a few parameters of the actual query, and keeps all the other parameters intact. For example if I'm on an URL like http://example.com/Posts/Index?Page=5&OrderBy=Name&OrderDesc=True I want to change only the Page, or OrderBy parameter and keep all other parameters the same, even those I don't yet know of (like when I want to add a Search parameter or something similar too).

The header of my current action looks like this:

public ActionResult Index(int? Page, string OrderBy, bool? Desc)

and I'm only interested in the values that this controller "eats". I want however that when I extend this action (for example with a string Search parameter) the links should work the same way as before.

Here is what I did already:

  1. Create a new RouteValueDictionary and fill it with everything from RouteData.Values
    • Problem: This only fills the parameters that are used in the Routing, so all other optional parameters (like Page) to the controller are lost
  2. Add everything from HttpContext.Request.QueryString to the previous dictionary
    • This is what I am currently using
    • Problem: It might have some junk stuff, that the Controller didn`t ask for, and it doesn't work if the page was loaded using POST. You also don't have any ModelBindings (but this isn't much of a problem, because we are re-sending everything anyway)
  3. Use HttpContext.Request.Params
    • Problem: this has too much junk data which imho one shouldn't add to a RouteValueDictionary that is passed to an ActionLink

So the questions:

  1. Is there an RVD that has all the data that was passed to the Controller and was used by it?
  2. Is this solution good, or are there any caveats I didn't think about (mainly in the context of changing a few query parameters while keeping the others intact)?
  3. Is there a way to filter out the "junk" data from the Params object?

EDIT: Checked the RouteData.DataTokens variable, but it's usually empty, and doesn't contain everything I need. It seems to only contain parameters that are needed for the routing somewhere, but not all of the parameters.

A: 

Have a look in RouteData.DataTokens.

RouteData.DataTokens @ MSDN documentation:

Gets a collection of custom values that are passed to the route handler but are not used when ASP.NET routing determines whether the route matches a request.

HTHs,
Charles

Charlino
For me the Viewcontext.RouteData.DataTokens property is empty (Count==0), unless I define a route that needs some of the information I provided, but that's not my point. Seems these parameters aren't passed to the route handlers.
SztupY
This is still giving me issues. I have a URL like Source?investigationKey=2. From the RouteData.Values I can only get the controller and the action. I don't get "investigationKey" anywhere.Did anyone figure out how to get this?
gugulethun