开发者

HtmlHelper extension method for facebook like button

开发者 https://www.devze.com 2023-02-26 04:18 出处:网络
Is there an 开发者_开发技巧extension method for HtmlHelper that helps generating the code for facebook button or for open graph tags declaration?

Is there an 开发者_开发技巧extension method for HtmlHelper that helps generating the code for facebook button or for open graph tags declaration?

Thanks


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.


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

0

精彩评论

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