4

How I can pass a query string with special characters?

For example I need to pass "&" inside my query string as below:

../solrresults.asp?mode=search&data=M & S
3
  • 1
    Have you tried HttpUtility.UrlEncode() Commented Mar 6, 2012 at 6:48
  • 2
    For classic ASP, you can use Server.UrlEncode() Function... Commented Mar 6, 2012 at 7:05
  • Server.UrlEncode() works for most of the special characters.For "%" it is not working Commented Mar 6, 2012 at 7:36

3 Answers 3

6

Use Server.UrlEncode:

URLEncode converts characters as follows: Spaces ( ) are converted to plus signs (+). Non-alphanumeric characters are escaped to their hexadecimal representation.

Use it this way;

<a href="page2.asp?name=<%= Server.URLEncode(sName) %>">here</a>
Sign up to request clarification or add additional context in comments.

1 Comment

My code is in classic ASP, so I would like to see a way in ASP itself
-1

As stated, the UrlEncode method would be the way to go.

Some time ago I wrote a small class QueryString to simplify the work with those query strings.

An example usage would be:

private void Page_Load(
    object sender, 
    System.EventArgs e )
{
    // Let the object fill itself 
    // with the parameters of the current page.
    QueryString qs = new QueryString();

    // Read a parameter from the QueryString object.
    string value1 = qs["name1"];

    // Now remove the parameter.
    qs.RemoveParameter( "name1" );

    // This has the same effect as RemoveParameter() method:
    qs["name1"] = null;

    // ... Further processing of the value1 variable ... 
}

You can fill the query string class and don't have to care whether to URL-encode the values, the class handles this internally for you.

1 Comment

My code is in classic ASP, so I would like to see a way in ASP itself
-1

After your comments, here is a solution in various scripting languages:

There are different versions available for each scripting language. Here is a reference from w3schools:

In JavaScript you can use the encodeURI() function. PHP has the rawurlencode() function and ASP has the Server.URLEncode() function.

Reference: http://www.w3schools.com/tags/ref_urlencode.asp

1 Comment

My code is in classic ASP, so I would like to see a way in ASP itself

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.