views:

165

answers:

5

I am trying to allow a user to specify a background color for my asp.net website. So I want to pass the page color into my url like:

UserDetails.aspx?Color=#FFFFFF

However as soon as I include the # character I can't seem to be able to get the correct value for the parameter Color using:

color = System.Drawing.ColorTranslator.FromHtml(Request.Params["Color"]);

Please help

+5  A: 

The pound character (#) is used in URLs to indicate where a fragment identifier (bookmarks/anchors in HTML) begins. You'll probably want to URL encode it as %23.

Or, as Andrew noted in a comment, you could have the user pass the parameter without the pound sign and add it in later.

cakeforcerberus
or skip the url encoding and just have them pass through the hex code without the #. Then add the # sign back in in your code
andrewWinn
A: 

or you coould just omit the hash and write like this

color = System.Drawing.ColorTranslator.FromHtml("#" + Request.Params["Color"]);

ive not tested the above, but find no reason for it not to work, you could decalre this above maybe too

minus4
+4  A: 
UserDetails.aspx?Color=FFFFFF

string colString = Request["Color"];
Color col = ColorTranslator.FromHtml(String.Format("#{0}", colString));
yodaj007
A: 

If you are modifying the querystring client side, I would make use of the JavaScript encodeURIComponent method to encode all of your data.

RedFilter
A: 

For future reference, be aware of the HttpUtility.UrlEncode Method. All query string parameters should be URL encoded before being passed via URL. "URL encoding converts characters that are not allowed in a URL into character-entity equivalents." eg.

string colour = HttpUtility.UrlEncode("#FFFFFF");
string url = "UserDetails.aspx?Color=" + colour;
Dan Diplo