views:

23

answers:

1

Hi,

I put the facebook "like this" widget on my pages and i have a problem:

My url is dynamically constructed with this method:

protected String getCurrentUrl()
{
    return Server.HtmlEncode(Request.Url.AbsoluteUri);
}

so the facebook iframe is:

<iframe src="http://www.facebook.com/plugins/like.php?href=&lt;%= getCurrentUrl() %>&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=80"
scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: 450px;
height: 80px;" allowtransparency="true"></iframe>

Well, an example of the getCurrentUrl result is:

http://gramma.ro/Site/DetaliiProdus.aspx?c=m1&amp;p=427&amp;s1=41&amp;s2=72

But, because of those "&" (& - when encoded) facebook does not correctly grab the link:

  • it takes:

http://gramma.ro/Site/DetaliiProdus.aspx?c=m1

instead of the whole one of the above...so it stops at the first &.

Do you see any workaround?

A: 

You probably want to Url encode:

return Server.UrlEncode(Request.Url.AbsoluteUri);

Although I would suggest you generating this url with something among the lines:

public string GetFaceBookAddress()
{
    var builder = new UriBuilder("http://www.facebook.com/plugins/like.php");
    var nvc = new NameValueCollection
    {
        { "href", Request.Url.AbsoluteUri },
        { "layout", "standard" },
        { "show_faces", "true" },
        { "width", "450" },
        { "action", "like" },
        { "font", "verdana" },
        { "colorscheme", "light" },
        { "font", "80" },
    };
    builder.Query = ToQueryString(nvc);
    return builder.ToString();
}

private static string ToQueryString(NameValueCollection nvc)
{
    return string.Join("&", Array.ConvertAll(nvc.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))));
}

And then:

<iframe src="<%= Server.HtmlEncode(GetFaceBookAddress()) %>" ...
Darin Dimitrov
Thanks a lot....! It works....
Cristian Boariu
@Christian, please see my update about a suggestion of using an improved function.
Darin Dimitrov