encoding html string in asp.net

Hi,

I am working on asp.net 2.0 using C#

I need to encode url. In my current html, I am writting below url in my imc src (tracking pixel)

so in html generated by .net has the code

<img src="below url" />

Current URL:
https://domain.com/a/r2068132441/FTG.gif?a=b499309-q499308&b=128400&c=ae-dxb-jumeirah beach hotel&amp;d=OK&amp;e=1

Required URL:
https://domain.com/a/r2068132441/FTG.gif?a=b499309-q499308&b=128400&c=ae-dxb-atlantis%20the%20palm%20dubai&d=OK&e=1


Above Url has:
&amp; instead of &
Where ever there is space in the string there it is required (%20)

Please see the difference in Current and Required URL.

Code below:

HtmlGenericControl ctrlImage = new HtmlGenericControl("img");

ctrlImage.Attributes["src"] = String.Format("https://{0}/a/r{1}/{2}.gif?a={3}&b={4}&c={5}&d={6}&e={7}",DynConfig.Current["SdddfeMail/ClientDomain"],
DynConfig.Current["SdddfeMail/AID"],
DynConfig.Current["SdddfeMail/ClientName"], A, B, C, D, E);
ctrlImage.Attributes["width"] = "1px";
ctrlImage.Attributes["height"] = "1px";
this.Controls.Add(ctrlImage);

Open in new window


           
Where ABCDE are the properties example below

private string _a = string.Empty;


[Bindable(true),
Category("Data"),
DefaultValue(""),
Description("Unique ID")]
public string A
{
    get { return _a; }
    set { _a = value; }
}

Open in new window


Please advice.

Regards,
tia_kamakshiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
The &amp; you are seeing in the page source is not actually 'transmitted' to retrieve the image. It is part of the correct way to encode HTML text and attributes. You can check this by looking at the properties of the image (in IE: right click on it).

Also, instead of %20 you would normally see a + to encode a space in a querystring parameter.

You could use this code:
            HtmlGenericControl ctrlImage = new HtmlGenericControl("img");

            ctrlImage.Attributes["src"] = String.Format("https://{0}/a/r{1}/{2}.gif?a={3}&b={4}&c={5}&d={6}&e={7}",
                DynConfig.Current["SdddfeMail/ClientDomain"],
                DynConfig.Current["SdddfeMail/AID"],
                DynConfig.Current["SdddfeMail/ClientName"],
                HttpUtility.UrlEncode(A),
                HttpUtility.UrlEncode(B),
                HttpUtility.UrlEncode(C),
                HttpUtility.UrlEncode(D),
                HttpUtility.UrlEncode(E));
            ctrlImage.Attributes["width"] = "1px";
            ctrlImage.Attributes["height"] = "1px";
            this.Controls.Add(ctrlImage);

Open in new window

If you really want %20 instead of + just use this code on line 9:
                HttpUtility.UrlEncode(C).Replace("+", "%20"),

Open in new window

0
tia_kamakshiAuthor Commented:
Thanks but code doesn't work.
Also, I tried below code and this is still not working.
Please advise.

Output for below code is:
	https://domainname.com/a/r2068132441/FTG.gif?a=q499346&amp;b=1703.00&amp;c=jumeirah beach hotel&amp;d=OK&amp;e=1

Open in new window


HtmlGenericControl ctrlImage = new HtmlGenericControl("img");

string decodeSpace = DynConfig.Current["SdddfeMail/DecodeSpaceInUrl"];

string strUrl = String.Format("https://{0}/a/r{1}/{2}.gif?a={3}&b={4}&c={5}&d={6}&e={7}", DynConfig.Current["SdddfeMail/ClientDomain"],
	DynConfig.Current["SdddfeMail/AID"],
	DynConfig.Current["SdddfeMail/ClientName"], A, B, C, D, E);

	string strFinalurl  = string.Empty;
	if(!string.IsNullOrEmpty(decodeSpace))
	{
		strFinalurl = HttpUtility.HtmlDecode(strUrl);
	}
	else if (decodeSpace.ToLower().Equals("true"))
	{
		strFinalurl = HttpUtility.HtmlDecode(strUrl).Replace(" ", "%20");
	}
	else
	{
		strFinalurl = HttpUtility.HtmlDecode(strUrl);
	}

	ctrlImage.Attributes["src"] = strFinalurl;
	ctrlImage.Attributes["width"] = "1px";
	ctrlImage.Attributes["height"] = "1px";
	this.Controls.Add(ctrlImage);

Open in new window

0
Robert SchuttSoftware EngineerCommented:
Please look carefully at the code I posted before. You need to encode the variables A, B, C, D and E separately, not after creating the string, but before/inside the string.format command like I did it, and not use decode here at all.

I purposely tested with variables as you created them and this generates the needed url in my experience and this test project I made. Please try the code as I posted it because I made it exactly so that you don't have to change anything, just copy/paste this instead of your current code as you posted, the first code block in your question.
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

tia_kamakshiAuthor Commented:
Thanks,
I will come back to you shortly in about 2 hrs on this.


Regards,
0
tia_kamakshiAuthor Commented:
I pasted your code. Please see the output below:

<img src="https://domainname.com/a/r2068132441/FTG.gif?a=q499351&amp;b=1684.00&amp;c=jumeirah+beach+hotel&amp;d=OK&amp;e=1" width="1px" height="1px"></img>

Open in new window


Code:


HtmlGenericControl ctrlImage = new HtmlGenericControl("img");

            ctrlImage.Attributes["src"] = String.Format("https://{0}/a/r{1}/{2}.gif?a={3}&b={4}&c={5}&d={6}&e={7}",
                DynConfig.Current["SdddfeMail/ClientDomain"],
                DynConfig.Current["SdddfeMail/AID"],
                DynConfig.Current["SdddfeMail/ClientName"],
                HttpUtility.UrlEncode(A),
                HttpUtility.UrlEncode(B),
                HttpUtility.UrlEncode(C),
                HttpUtility.UrlEncode(D),
                HttpUtility.UrlEncode(E));
            ctrlImage.Attributes["width"] = "1px";
            ctrlImage.Attributes["height"] = "1px";
            this.Controls.Add(ctrlImage);

Open in new window


Please advise
0
Robert SchuttSoftware EngineerCommented:
Yes that's correct. This url should load the image with the correct parameters. Of course the results will only be evident after analyzing the server statistics/weblogging or looking at the data that is being collected directly by downloading that gif (if that's what it's doing).

To make extra clear why I think this is what it should be I made a screen print of my project in debug mode where I also added an <a href> with the same url as the <img src> and you can see when you hover over the link (or look at the image properties) that the url does not actually contain &amp; but just &
capture
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.