How to URLEncode an Apostrophe (')

D

David

When I try to URLEncode the following example string:

Dave's Company

I get the following translation:

Dave's+Company


In other words, the apostrophe is not translated into it's ascii value
of '%27'. Now, if I try to use this string as part of a name/value
pair in a hyperlink on my page, my browser incorrectly interprets the
apostrophe as a quote ('). This results in everything after the
apostrophe being cut off when someone clicks on that hyperlink.

I would think URLEncode would encode apostrophes to avoid this issue.
However, I've found that I have to manually replaces apostrophes with
'%27' in my strings.

Is this the way it has to be or am I doing something wrong or is there
an easier way?

Dave
 
H

Herfried K. Wagner [MVP]

(e-mail address removed) (David) scripsit:
When I try to URLEncode the following example string:

Dave's Company

I get the following translation:

Dave's+Company


In other words, the apostrophe is not translated into it's ascii value
of '%27'. Now, if I try to use this string as part of a name/value
pair in a hyperlink on my page, my browser incorrectly interprets the
apostrophe as a quote ('). This results in everything after the
apostrophe being cut off when someone clicks on that hyperlink.

I would think URLEncode would encode apostrophes to avoid this issue.
However, I've found that I have to manually replaces apostrophes with
'%27' in my strings.

'UrlEncode' will encode the URL for use with HTTP, you want to encode it
for use in a HTML page. IMHO you will have to write your own routine...
 
T

Tom Shelton

(e-mail address removed) (David) scripsit:

'UrlEncode' will encode the URL for use with HTTP, you want to encode it
for use in a HTML page. IMHO you will have to write your own routine...

Sorry, but I can't see the original post - so I am going to reply
Herfried's...

Check out the HtmlEncode method of System.Web.HttpUtility class. I
haven't tested it, since I don't have acess to my windows partition at
the moment :) But, I think it may do what you want.

HTH,
Tom Shelton
 
C

Cor

Hi,
I did not even know it exist. Nak had a question about it some weeks ago.
The only thing we knew then was the mshtml.URLUnencoded and he would be able
to use Urldecode.

I tried this one yesterday, but was waiting for a better answer.
I have got the same situation while trying it and I tried a lot of encoding
tables also.
The ' is always a '. I am curious if someone has a solution, this is one of
the most important ones and who knows what is more.
Because this one can be seen direct, but what unexpected can happen more.
In this situation a command like this has absolutly to be avoiding in my
opinion.

Cor.
 
D

Don B

(e-mail address removed) (David) scripsit:

'UrlEncode' will encode the URL for use with HTTP, you want to encode it
for use in a HTML page. IMHO you will have to write your own routine...



Response.Redirect("WebPage.aspx?WhateverQueryString=") &
Server.UrlEncode(YourString)

On the other end,
strPassed = Server.UrlDecode(Request.QueryString("WhateverQueryString"))


Might work, might not. Haven't tried it.
 
D

Dave M.

It appears that neither URLEncode() or HTMLEncode() in the
System.Web.HttpUtility class encodes an apostrophe. It's looking like I
have no other choice but to do it manually. This seems like an
oversight to me on Microsoft's part.
 
H

Herfried K. Wagner [MVP]

Dave M. said:
It appears that neither URLEncode() or HTMLEncode() in the
System.Web.HttpUtility class encodes an apostrophe. It's looking like I
have no other choice but to do it manually. This seems like an
oversight to me on Microsoft's part.

In general, the "'" character doesn't need to be encoded in HTML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top