0

In call the following URL in Javascript.

var par = "Participant.aspx?ID=" + Id + "&NAME=" + Name+ "&FIRSTNAME=" + Firstname;

Sometimes the Name or the Firstname contains an "ampersand" or an "plus" sign.

For Example:        Richard & Michael or Richard + Michael

On the server side, I read the query string like this:

        Dim Name As String = Request.QueryString("NAME")
        Dim Firstname As String = Request.QueryString("FIRSTNAME")

My problem is that if the query string contains a "plus" sign, then the sign is converted to space ( ' ' ) and if the query string contains a "ampersand", then everything after the "ampersand" is deleted.

I have tried several things like. Request.Form instead of Reqeust.Querystring and i also tried Server.URLEncode. But both not working suitable.

With URLEncode is the problem that if the Querystring contains spaces between the Name and the "plus" sign, the spaces also get converted to "plus" signs.

Do you have an idea how to solve this?

2
  • 4
    With URLEncode is the problem that if the Querystring contains spaces between the Name and the "plus" sign, the spaces also get converted to "plus" signs. - that is what should happen. A plus sign denotes a space. An encoded plus sign denotes a plus sign. Sounds like you have already solved this problem. Commented Nov 12, 2014 at 9:39
  • @AntP: When I asked the question, the tags were javascript and c#. But yeah, the code sample is VB.Net. :-) Commented Nov 12, 2014 at 10:27

4 Answers 4

2

You can use encodeURIComponent for the query string value and then set it as query string

    var url = encodeURIComponent($("#<%=hdnPageQuery.ClientID%>").val());
    var title = encodeURIComponent(document.title);
    var redirectUrl = $("#<%=hdnPageTarget.ClientID%>").val();
    var outputUrl = redirectUrl + '?url=' + url + '&title=' + title;
    $('#ancSendToFriendLink').attr('href', outputUrl);
Sign up to request clarification or add additional context in comments.

Comments

1

Remember that the contents of the query string (both the names and values) must be correctly URI-encoded. If that line is in JavaScript, you'd do that like this:

var par = "Participant.aspx?ID=" + encodeURIComponent(Id) +
            "&NAME=" + encodeURIComponent(Name) +
            "&FIRSTNAME=" + encodeURIComponent(Firstname);

(Technically, again, the names should be encoded too, but "ID", "NAME", and "FIRSTNAME" encode to exactly the same thing, so I didn't use encodeURIComponent on them.)

See AntP's comment re the plus signs and spaces:

"With URLEncode is the problem that if the Querystring contains spaces between the Name and the "plus" sign, the spaces also get converted to "plus" signs." - that is what should happen. A plus sign denotes a space. An encoded plus sign denotes a plus sign.

1 Comment

Hmm, i think it was an cache problem. I deleted my cache and now it works as expected :). THANK YOU
1

You can use predefined UrlEncode and UrlDecode methods. These methods will help you out to pass special characters in query strings. Have a look at these examples.

UrlDecode and UrlEncode
hope this helps you.

Comments

-1

hey there is another way:

before passing it to query string just replace:

.Replace("&","%26");

on another page it automatically read %26 as &, but then also it not read as &, just again replace:

.Replace("%26","&");

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.