views:

544

answers:

3

By default the asp.net image control trys to be helpful and automatically encodes anything set to the ImageUrl property, so:

imgSomething.ImageUrl = "Generator.aspx?x=1&y=2&z=3";

Becomes

"Generator.aspx?x=1&y=2&z=3"

The problem is I want to pass Base64 encoded parameters, which I need to manually Server.UrlEncode because each one can contain charater that'll cause problems otherwise.

So basically my question is: How do I stop the Image control from automatically UrlEncoding what I set to the ImageUrl parameter?

A: 

Put an @ symbol in front of the string. For example:

imgSomething.ImageUrl = @"Generator.aspx?x=1&y=2&z=3";
Chris Lively
The @ before the string disables the C escape sequences in the string, not the escaping done by the code of the control.
Matteo Italia
+1  A: 

We ran into the same issue. Our workaround was to package all the parameters into one URLEncoded and Base64-encoded parameter, and split it ourselves on the other side. We notice a similar approach in WebResource.axd and ScriptResource.axd.

Quick and dirty way (using simple helper methods for Base64 encoding/decoding):

string parameters = args.Join('|');
imgSomething.ImageUrl = "Generator.aspx?d=" + Server.UrlEncode(Base64Encode(parameters));

in Generator.aspx:

string data = Base64Decode(Server.UrlDecode(Request.QueryString["d"].ToString().Trim()));
string[] parameters = data.Split('|');

If you want to use querystring-style parameter strings (i.e., x=1&y=2&z=3), there is a bunch of sample code out there that will let you move between a string and NameValueCollection.

chaiwalla
+1  A: 

I think you should use server control. not asp control.

<img ID="Image2" src="" alt="" runat="server" />
Me.Image2.Src = "&&&"

it works.

Good idea! :) That never even crossed my mind
Peter Bridger