当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - HtmlHelper extension method for facebook like button

问题描述:

Is there an extension method for HtmlHelper that helps generating the code for facebook button or for open graph tags declaration?

Thanks

网友答案:

You can try with helpers in Microsoft.Web.Helpers.dll just download the library with NuGet.

Here's a link: http://weblogs.asp.net/imranbaloch/archive/2010/11/07/using-asp-net-web-pages-in-asp-net-mvc.aspx

网友答案:

I had a similar scenario where I had to create a Facebook like button (using iframe only) and had no need of any other assembly, this is what I ended up doing:

Code:

    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);
    }

Usage:

@Html.FacebookLikeButton(Model.AbsoluteUrl)

Since this post is quite old I guess you've managed to achieve what you were hoping for, although this post was the first result hit I got when searching for the topic.

分享给朋友:
您可能感兴趣的文章:
随机阅读: