开发者

facebook like button iframe with dynamic url printed in asp.net razor templates

开发者 https://www.devze.com 2023-03-22 20:50 出处:网络
I have a asp.net website in the umbraco cms, there I can use razor templating to create some macros. I want to integrate a facebook like button in the page,

I have a asp.net website in the umbraco cms, there I can use razor templating to create some macros. I want to integrate a facebook like button in the page, this works ONLY when I paste the intire iframe in a razor macro and incorporate the macro on my masterpage.

Though, if I want to include some parameters, and I take the iframe URL out of the code snippet to paste another parameter inside that url it stops working.

However, after rendering both the iframe url is 100% identical.

Anyone got an idea on how I can make sure that the correct site url is in that Facebook iframe parameter can be changed? (depending on the hostname I bind in the umbraco cms)

Here is my razor code that does not work:

@{
  var currentLangPath = "www.newurl.com"; 
  var iframeSource = "http://www.facebook.com/plugins/like.php?app_id=207125959336150&href=" + currentLangPath + "&send=false&layout=button_count&width=110&show_faces=false&action=like&colorscheme=dark&font=arial&height=21";      
  }

<a href="#visitFanpage"><img src="/images/facebook_logo.jpg" alt="Solex Facebook" /></a>
<p>Become part of the community, and feel free to share your experience.</p>
<div class="social">
  <iframe src="@iframeSource" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:25px;"开发者_开发百科 allowTransparency="true"></iframe>
</div>

Here is the content of a Razor file that does work:

<a href="#visitFanpage"><img src="/images/facebook_logo.jpg" alt="Solex Facebook" /></a>
<p>Become part of the community, and feel free to share your experience.</p>
<div class="social">
  <iframe src="http://www.facebook.com/plugins/like.php?app_id=207125959336150&amp;href=www.newurl.com&amp;send=false&amp;layout=button_count&amp;width=110&amp;show_faces=false&amp;action=like&amp;colorscheme=dark&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:25px;" allowTransparency="true"></iframe>
</div>

Note that both these Razor files have EXACTLY the same output, so I find it very weird that the dynamic URL one does not work.


The output isn't actually the same, the iframeSource variable gets HTML encoded and therefore an "&amp;" turns into:

&amp;amp;

You could do two things:

  1. Remove "amp;" after each "&" in the iframeSource
  2. Instead of @iframeSource, use @Html.Raw(iframeSource)
0

精彩评论

暂无评论...
验证码 换一张
取 消