Greg
Greg

Reputation: 34798

URL-encode a URL

Is there a .NET encoding method I could use to encode a URL to be passed within a URL parameter?

For example say I have:

url_of_interest = "http://asdf.asdf/asdf.htm"

and I want to include this as one (1) URL form parameter when I do an upload to a web-application:

http://mywebservice/upload?url=<<encoded URL here>>

Upvotes: 6

Views: 2587

Answers (5)

Boris Lipschitz
Boris Lipschitz

Reputation: 9516

As System.Web is being decommissioned from a modern OWIN based application, the alternative solution to HttpUtility.UrlEncodeshould be WebUtility.UrlEncode

Upvotes: 1

TrustyCoder
TrustyCoder

Reputation: 4789

You should use MS Anti XSS library's

AntiXss.UrlEncode method

The AntiXSS library can be downloaded from the following location

http://www.microsoft.com/downloads/details.aspx?familyid=051EE83C-5CCF-48ED-8463-02F56A6BFC09&displaylang=en

Upvotes: 1

leepowers
leepowers

Reputation: 38298

HttpServerUtility.UrlEncode should do the trick:

http://msdn.microsoft.com/en-us/library/zttxte6w.aspx

Upvotes: 2

Chris Fulstow
Chris Fulstow

Reputation: 41872

HttpUtility.UrlEncode

The UrlEncode() method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when embedded in a block of text to be transmitted in a URL, the characters < and > are encoded as %3c and %3e.

Upvotes: 1

Related Questions