How to get all active parameters in ASP.NET MVC (2)

I was wondering if there is a way to create ActionLink

or a similar one that only changes a few parameters of the actual request and keeps all other parameters intact. For example, if I have a url, for example http://example.com/Posts/Index?Page=5&OrderBy=Name&OrderDesc=True

, I only want to change a parameter Page

or OrderBy

and keep all other parameters the same, even the ones I don't know about yet (for example, when I want to add a parameter Search

or something like that).

The title of my current activity looks like this:

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

      

and I am only interested in the values ​​that this controller "eats". However, I want that when extending this action (with a parameter, for example string Search

) the links should work the same as before.

Here's what I've already done:

  • Create a new one RouteValueDictionary

    and fill it with everything fromRouteData.Values

    • Problem. This only fills in the parameters that are used in routing, so all other optional parameters (for example Page

      ) for the controller are lost.
  • Add all from HttpContext.Request.QueryString

    to the previous dictionary
    • This is what I am currently using
    • Problem: It might have some unwanted things that the controller didn't ask for, and it doesn't work if the page was POST loaded. You also don't have ModelBindings (but that's not a big problem because we're forwarding everything anyway)
  • Use HttpContext.Request.Params

    • Problem: It has too much garbage data that imho shouldn't be added to RouteValueDictionary

      which is being passed toActionLink

So the questions:

  • Is there one RVD

    that has all the data that was transmitted to the controller and was used by it?
  • Is this a good solution, or are there any caveats that I haven't thought of (mostly in the context of modifying multiple query parameters while keeping others intact)?
  • Is there a way to filter out "unwanted" data from an object Params

    ?

EDIT: Checked the variable RouteData.DataTokens

, but it's usually empty and doesn't contain everything I need. It seems to contain only the parameters required for routing, but not all parameters.

+2


a source to share


1 answer


Take a look 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 if a route matches a request.

HTHS,
Charles

-1


a source







All Articles