views:

55

answers:

4

I know i can do this

var nv = HttpUtility.ParseQueryString(req.RawUrl);

But is there a way to convert this back to a url?

var newUrl = HttpUtility.Something("/page", nv);
A: 

You can use.

var ur = new Uri("/page",UriKind.Relative);

if this nv is of type string you can append to the uri first parameter. Like

var ur2 = new Uri("/page?"+nv.ToString(),UriKind.Relative);
Gabriel Guimarães
+nv ... that wont escape properly.
acidzombie24
What about now?
Gabriel Guimarães
+1  A: 

Would this work for you?

public static string BuildUrl(string relativeUrl, params string[] queryString)
{
    // build queryString from string parameters
    char[] trimChars = { ' ', '&', '?' };
    StringBuilder builder = new StringBuilder();
    string sepChar = "&";
    string sep = String.Empty;
    foreach (string q in queryString)
    {
        builder.Append(sep).Append(q.Trim(trimChars));
        sep = sepChar;
    }

    if (String.IsNullOrEmpty(builder.ToString())) { return relativeUrl; }
    else { return relativeUrl + "?" + builder.ToString(); }
}

Use:

string url = BuildUrl("/mypage.apsx", "qs1=a", "qs2=b", "qs3=c");
Brad
+1 for trying but that wont solve my need. I'll paste you some code if i write an implementation.
acidzombie24
+1  A: 

Simply calling ToString() on the NameValueCollection will return the name value pairs in a name1=value1&name2=value2 querystring ready format. You can then append it to the URL and redirect.

var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
string url = Request.Url.AbsolutePath + "?" + nameValues.ToString();
Response.Redirect(url);

As an aside, you can call the Add, Set, and Remove methods on nameValues to modify any of the querystring items before appending it. If you're interested in that see my response to another question.

EDIT: to encode the query string items you'll have to use the HttpUtility.UrlEncode method. Better yet, grab the Microsoft Anti-Cross Site Scripting Library (currently at V3.1) which provides additional and stricter variants of encoding methods.

You could use this code to build up the encoded query string:

var qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
var sb = new StringBuilder("?");
for (int i = 0; i < qs.Count; i++)
{
    sb.AppendFormat("{0}={1}", qs.Keys[i], HttpUtility.UrlEncode(qs[i]));
    if (i < qs.Count - 1)
        sb.Append("&");
}

string url = Request.Url.AbsolutePath + sb.ToString();
Response.Redirect(url);

Note that I initialized the StringBuilder with the ? character so I no longer concatenate it to the url as I did earlier.

Ahmad Mageed
I'm unsure nameValues.ToString() will escape properly.
acidzombie24
@acidzombie see my updates.
Ahmad Mageed
+2  A: 

The short answer is to use .ToString() on the NameValueCollection and combine it with the original url.

However, id like to point out a few things.

You cant use HttpUtility.ParseQueryString on Request.RawUrl. The ParseQueryString method is looking for a value like this "?var=value&var2=value2".

If you want to get a NameValueCollection of the QueryString parameters just use Request.QueryString.

var nv = Request.QueryString;

To rebuild the URL just use nv.ToString().

string url = String.Format("{0}?{1}", Request.Path, nv.ToString());

If you are trying to parse a url string instead of using the Request object use Uri and the HttpUtility.ParseQueryString method.

Uri uri = new Uri("<THE URL>");
var nv = HttpUtility.ParseQueryString(uri.Query);
string url = String.Format("{0}?{1}", uri.AbsolutePath, nv.ToString());
Jeremy