views:

210

answers:

2

Assume the following Url:

"http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents & Functions + Properties.docx&Save=true"

I use HttpUtility.UrlEncode() to encode the value of the Filename parameter and I create the following Url:

"http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents%20%26%20Functions%20%2B%20Properties.docx&Save=true"

I send the following (encoded version) of request from a client to a C# Web Application. On the server when I process the request I have a problem. The HttpRequest variable contains the query string partially decoded. That is to say when I try to use or quick watch the following properties of HttpRequest they have the following values.

Property = Value
================
HttpRequest.QueryString = "{Library=Testing&Filename=Documents+&+Functions+++Properties.docx&Save=true}"

HttpRequest.Url = "{http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents & Functions + Properties.docx&Save=true}"

HttpRequest.Url.AbsoluteUri = "http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents%20&%20Functions%20+%20Properties.docx&Save=true"

I have also checked the following properties but all of them have the & value decoded. However all other values remain properly encoded (e.g. space is %20).

HttpRequest.Url.OriginalString

HttpRequest.Url.Query

HttpRequest.Url.PathAndQuery

HttpRequest.RawUrl

There is no way I can read the value of the parameter Filename properly. Am I missing something?

+2  A: 

The QueryString property returns a NameValueCollection object that maps the querystring keys to fully-decoded values.

You need to write Request.QueryString["FileName"].

SLaks
Thanks for the answer but this did not work.
Ioannis
Ioannis
A: 

What happens when you don't use UrlEncode? You didn't show how exactly you are using the url that you created using UrlEncode, so it is quite possible that things are just being double encoded (lots of the framework will encode the URLs for you automatically).

Stephen M. Redd
Ioannis
Anyway, I figured out a solution, I manually encode the value of the Filename with my custom encoder/decoder and it works fine.
Ioannis