Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop asp.net image control automatically UrlEncoding the ImageUrl parameter

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?

like image 508
Peter Bridger Avatar asked Feb 28 '23 16:02

Peter Bridger


2 Answers

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

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

it works.

like image 152
user148826 Avatar answered Mar 05 '23 17:03

user148826


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.

like image 37
ultravelocity Avatar answered Mar 05 '23 15:03

ultravelocity