Arnold Zokas
Arnold Zokas

Reputation: 8560

Querystring with url-encoded ampersand prematurely decoded within Request.Url

Given url /Page.aspx?a=b&title=apples+%26+pears, Request.Url property returns /Page.aspx?a=b&title=apples+&+pears

Note that the url-encoded ampersand in the second key-value pair has been automatically decoded. Other url-encoded reserved characters aren't being automatically decoded.

Is this behaviour correct?

EDIT: The issue is that Request.Url property is automatically decoding the encoded ampersand when I don't expect it to.

ANSWER: string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Host, Sitecore.Context.Request.RawUrl)

Upvotes: 2

Views: 2557

Answers (2)

Aliostad
Aliostad

Reputation: 81660

Url property of the Request gets decoded in an internal method called CollapsePercentUFromStringInternal.

You can see this in the reflector. This I assumed is the default behaviour anyway.

Update

You can use RawUrl property to get hold of the un-decoded URL.

Upvotes: 2

FosterZ
FosterZ

Reputation: 3911

Reserved characters URLs use some characters for special use in defining their syntax. When these characters are not used in their special role inside a URL, they need to be encoded.

 
 Dollar          ("$")
 Ampersand       ("&")
 Plus            ("+")
 Comma           (",")
 Forward slash   ("/")
 Colon           (":")
 Semi-colon      (";")
 Equals          ("=")
 Question mark   ("?")
 'At' symbol     ("@")

UnSafe charracters

Some characters present the possibility of being misunderstood within URLs for various reasons. These characters should also always be encoded.

Percent character ("%")

'Pound' character ("#")

Less Than' symbol ("<") 'Greater Than' symbol (">") Space

So the behaviour of URL encoding is correct..

Upvotes: 2

Related Questions