Adding Social Buttons, Twitter, facebook, Google +1, to ASP.NET MVC
An easy way to add social buttons to your site is to have a nice and portable ASP.NET MVC HTML Helper. According to the documentation for Tweet button, Facebook Like button, and Google +1 button, listed below is the code to create these social buttons and embed them into any site.
Required Scripts
Include the following script into your _layout page.
$(function () {
//Google +1
$.getScript("http://apis.google.com/js/plusone.js", null, true);
//Twitter
$.getScript("http://platform.twitter.com/widgets.js", null, true);
//Facebook
$.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1", function(){
$('body').append('<div id="fb-root"></div>');
FB.init({ status: true, cookie: true, xfbml: true });
}, true);
});
and the following are the HTML Helpers for each social button.
Tweet Button
<Extension()>
Function Social_Twitter(htmlHelper As HtmlHelper, title As String, _
Optional url As String = "") As MvcHtmlString
Dim social_link As New TagBuilder("a")
social_link.Attributes.Add("href", "https://twitter.com/share")
social_link.Attributes.Add("class", "twitter-share-button")
social_link.Attributes.Add("data-via", "MY-TWITTER-HANDLE")
social_link.Attributes.Add("data-count", "horizontal")
social_link.Attributes.Add("data-text", title)
social_link.SetInnerText("Tweet")
If Not String.IsNullOrEmpty(url) Then
social_link.Attributes.Add("data-url", url)
End If
Return New MvcHtmlString(social_link.ToString(TagRenderMode.Normal))
End Function
Facebook Like
<Extension()>
Function Social_Facebook(htmlHelper As HtmlHelper, title As String, _
Optional url As String = "") As MvcHtmlString
Dim str = New StringBuilder
Dim social_link As New TagBuilder("div")
social_link.Attributes.Add("class", "fb-like")
social_link.Attributes.Add("data-send", "false")
social_link.Attributes.Add("data-layout", "button_count")
social_link.Attributes.Add("data-show-faces", "false")
social_link.Attributes.Add("data-font", "arial")
If Not String.IsNullOrEmpty(url) Then
social_link.Attributes.Add("data-href", url)
End If
str.Append(social_link.ToString(TagRenderMode.Normal))
Return New MvcHtmlString(str.ToString)
End Function
Google +1 Button
<Extension()>
Function Social_GooglePlusOne(htmlHelper As HtmlHelper, title As String, _
Optional url As String = "") As MvcHtmlString
Dim social_link As New TagBuilder("div")
social_link.Attributes.Add("class", "g-plusone")
social_link.Attributes.Add("data-size", "medium")
'social_link.Attributes.Add("data-size", "small")
If Not String.IsNullOrEmpty(url) Then
social_link.Attributes.Add("data-href", url)
End If
Return New MvcHtmlString(social_link.ToString(TagRenderMode.Normal))
End Function
You can also combine them all, and generate them with one line using the following code.
All Social Buttons Helper
<Extension()>
Function Social_AllButtons(htmlHelper As HtmlHelper, _
title As String, url As String) As MvcHtmlString
Dim str = New StringBuilder
Dim ul As New TagBuilder("ul")
ul.AddCssClass("social")
'Google
Dim li3 As New TagBuilder("li")
li3.InnerHtml = htmlHelper.Social_GooglePlusOne(title, url).ToHtmlString
li3.AddCssClass("social-google")
ul.InnerHtml += li3.ToString
'Twitter
Dim li2 As New TagBuilder("li")
li2.InnerHtml = htmlHelper.Social_Twitter(title, url).ToHtmlString
li2.AddCssClass("social-twitter")
ul.InnerHtml += li2.ToString
'facebook
Dim li1 As New TagBuilder("li")
li1.InnerHtml = htmlHelper.Social_Facebook(title, url).ToHtmlString
li1.AddCssClass("social-facebook")
ul.InnerHtml += li1.ToString
str.Append(ul.ToString(TagRenderMode.Normal))
Return New MvcHtmlString(str.ToString)
End Function
Now you can embed social buttons into any page, and if you do not set the URL, they get the current page URL by default.
You can also expand on these methods to add the required JavaScript source to run these buttons from their respective owners if it’s not included in the page. This way you can really separate all the dependencies into a totally portable utilities library.
发表评论
S5STWD Im obliged for the blog post.Really thank you! Great.
6wXWye Well I definitely liked studying it. This tip offered by you is very useful for proper planning.
Hi, thanks pro the info. I be deficient in to register you to look here
Say, you got a nice article.Thanks Again. Want more.
There is perceptibly a bunch to identify about this. I believe you made some good points in features also.
I really liked your blog article.Thanks Again. Really Cool.
Non Prescription Cialis telephone services with great service
This is a topic that is close to my heart Many thanks! Exactly where are your contact details though?
Thank you for your blog post. Fantastic.
This awesome blog is really interesting and informative. I have discovered a lot of interesting stuff out of this amazing blog. I ad love to visit it every once in a while. Thanks a lot!
I truly like your weblog put up. Keep publishing far more valuable details, we value it!
Muchos Gracias for your blog post.Thanks Again. Awesome.
Very good article post.Thanks Again. Cool.
It as not that I want to replicate your web site, but I really like the layout. Could you tell me which theme are you using? Or was it tailor made?
Im grateful for the post.Much thanks again. Awesome.
I was suggested this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are amazing! Thanks!
little bit acquainted of this your broadcast provided bright clear idea
Thanks for sharing, this is a fantastic blog post.Really thank you! Great.
I usually do not create a bunch of responses, however i did a few searching and wound up right here?? -
Utterly pent written content, Really enjoyed looking at.
Really appreciate you sharing this post.Really thank you! Awesome.
Wow! This blog looks just like my old one! It as on a entirely different topic but it has pretty much the same page layout and design. Wonderful choice of colors!
This is one awesome article post.Much thanks again. Much obliged.
Such interesting stuff and reporting! Keep up the good work guys
Your golfing ask to help you arouse your recollection along with improve the
Your content is excellent but with pics and videos, this blog could undeniably be one of the best in its field.
Really enjoyed this post.Really looking forward to read more.
Perfectly written content, thanks for selective information.
Maybe you can write subsequent articles relating to this
Me English no outstanding, but had to say me like what you say. Thank you from me.
the original materials to represent skin etc. As for the technology of air cushion; it is useful
just beneath, are quite a few totally not connected web-sites to ours, even so, they may be surely really worth going over
Thanks for the article.Thanks Again. Want more.
Major thankies for the article.Really thank you! Much obliged.
My brother recommended I might like this blog. He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!
The web is really useful for me and all people studying English. The quality of podcast is good and I hope there will be more useful and interesting lessons. Thanks!
Please let me know if you are looking for a author for your site. You have some really good posts and I think I would be a good asset. If you ever want to take some of the load off, I ad absolutely
Thanks for sharing, this is a fantastic blog.Really thank you! Fantastic.
It as not that I want to copy your internet site, but I really like the layout. Could you let me know which style are you using? Or was it tailor made?
This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your fantastic post. Also, I ave shared your website in my social networks!
ugg australia women as fringe cardy boot
You made some decent points there. I looked on the web to find out more about the issue and found most individuals will go along with your views on this web site.
I will right away grab your rss feed as I can not find your email subscription link or e-newsletter service. Do you ave any? Please allow me know in order that I could subscribe. Thanks.
This particular blog is really interesting as well as factual. I have chosen helluva helpful things out of this source. I ad love to return every once in a while. Thanks a lot!
thank you for all your efforts that you have put in this. Very interesting info. Truth sits upon the lips of dying men. by Matthew Arnold.
Looking forward to reading more. Great blog.Really thank you!
Way cool! Some extremely valid points! I appreciate you penning this post and also the rest of the site is extremely good.
Im obliged for the article post. Really Cool.
There as definately a great deal to find out about this issue. I like all the points you have made.
Really appreciate you sharing this blog.Really looking forward to read more. Keep writing.