2011-04-18 143 views

回答

3

我也有類似的情況,我必須創建一個Facebook像按鈕(只能用IFRAME),也沒有任何有需要的其他組件的,這是我落得這樣做:

代碼:

public static MvcHtmlString FacebookLikeButton(this HtmlHelper htmlHelper, string url, int width = 90, int height = 21, object htmlAttributes = null) 
    { 
     var tagBuilder = new TagBuilder("iframe"); 

     var uriBuilder = new UriBuilder("http://www.facebook.com/plugins/like.php"); 

     var nvc = new NameValueCollection 
     { 
      {"locale", Thread.CurrentThread.CurrentCulture.ToString().Replace("-", "_")}, 
      {"href", url}, 
      {"layout", "button_count"}, 
      {"show_faces", "true"}, 
      {"width", width.ToString(CultureInfo.InvariantCulture)}, 
      {"height", height.ToString(CultureInfo.InvariantCulture)}, 
      {"action", "like"}, 
      {"colorscheme", "light"}, 
      {"font", "arial"} 
     }; 

     uriBuilder.Query = string.Join("&", Array.ConvertAll(nvc.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key])))); 

     tagBuilder.MergeAttribute("src", uriBuilder.Uri.AbsoluteUri); 
     tagBuilder.MergeAttribute("scrolling", "no"); 
     tagBuilder.MergeAttribute("frameborder", "0"); 
     tagBuilder.MergeAttribute("style", string.Format("border:none; overflow:hidden; width:{0}px; height:{1}px;", width, height)); 
     tagBuilder.MergeAttribute("allowTransparency", "true"); 

     if (htmlAttributes != null) 
     { 
      tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 
     } 

     var completeHtml = tagBuilder.ToString(TagRenderMode.Normal); 

     return new MvcHtmlString(completeHtml); 
    } 

用法:

@Html.FacebookLikeButton(Model.AbsoluteUrl) 

由於這篇文章已經很老了,我猜你已經設法達到了你的期望,雖然這篇文章是我在搜索主題時獲得的第一個結果。

相關問題