6

I came across a very weird issue where in my querystirng had "++" as part of the text. but when i assign the query stirng value to a string ++ will become two spaces. How do i get exactly what is being passed as querystring?

I observed that the querystirng collection had "++" but when I do Request.QueryString["search"].ToString() "++" gone, I checked in Immediate window.

I use C# 2.0

URL: /default.aspx?search=test++

string t = Request.QueryString["search"].ToString();
1
  • + is usually translated to a space in url encoding. That´s why they disappear. Commented Dec 7, 2010 at 19:28

6 Answers 6

9

You should use UrlEncode and UrlDecode

Those methods should be used any time you're inserting user inputted data into the query string.

Sign up to request clarification or add additional context in comments.

2 Comments

And you can specify the encoding as well with those methods which can be useful :)
I was having an issue with my query string as well and specifically with the # symbol. Trying UrlEncode was actually not converting the # symbol to %23% although it is included in the specification as special characters! I finally had to perform an extra replace before i submitted the post. I hope that helps someone.
4

'+' is reserved in query strings.

Within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", and "$" are reserved.

Try using UrlEncode to encode your query strings.

1 Comment

Will, thanks for the update, we can handle all other characters with JS escape() function but not +, i had to do regex replace.
3

A plus sign in a query string translates to a space. If you want an actual plus sign rather than a space, use %2B instead.

/default.aspx?search=test%2B%2B

If you're doing this in code, then you should be using UrlEncode to encode this portion of the query string.

1 Comment

Thanks Adam... I had to do a regex replace on javascript for + with %2B, then call javascript escape(), and on C#, i had to replace %2b with + with regex and then do a Server.urlEncode()... all other characters are handled easily but not +
1

I don't know that there's a way to get the exact text passed into the query. The HTTP standards basically say that a + is equivalent to a space character, so if you want to preserve the + you should encode the query string, as Chuck said.

Comments

0

The only solution I found was in this post HERE:

private string GetQueryStringValueFromRawUrl(string queryStringKey)
{
    var currentUri = new Uri(HttpContext.Request.Url.Scheme + "://" +
                   HttpContext.Request.Url.Authority +
                   HttpContext.Request.RawUrl);
    var queryStringCollection = HttpUtility.ParseQueryString((currentUri).Query);
    return queryStringCollection.Get(queryStringKey);
}

Comments

0

Working on a ASP.Net 2.0 soluton, I had to do the following:

    private string GetParameterFromRawUrl(string parameter)
    {
        var rawUrl = Request.RawUrl;
        int indexOfParam = rawUrl.IndexOf(parameter);
        int indexOfNextParam = rawUrl.IndexOf('&', indexOfParam);
        string result;

        if (indexOfNextParam < 1)
        {
            result = rawUrl.Substring(indexOfParam);
        }
        else
        {
            result = rawUrl.Substring(indexOfParam, (indexOfNextParam-indexOfParam));
        }

        return result;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.