Capri82
Capri82

Reputation: 418

Create a String/Query String from RouteValueDictionary Object

I have created UrlHelper extension function(s) for different use(s). Now I am creating a function which will accept three parameters like:

url.cutomAction(action, controller, new RouteValueDictionary{{key,value},{..},.. });

In an extension function I will take the RouteValueDictionary and iterate over it like this :

foreach (var data in RouteData)
{
    data.Key, data.Value.ToString();
}

But now I need to know that while iterating over these keys and values how can I generate a string or query string like:

(key = value, key1 = value1, key2 = value2, ...)

so I can create a final URL and pass this string in it like:

return helper.Action(action, controller, new {above_Generated_string})

Or whatever is the right way to do it.

Upvotes: 0

Views: 3712

Answers (1)

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12324

There are couple of ways you can do it. One of them is using LINQ:

var result = RouteData.Select(s => string.Format("{0}={1}", s.Key, s.Value))
.Aggregate((current, next) => string.Format("{0}|{1}", current, next));

Upvotes: 2

Related Questions