Add facebook twitter and google plus in C# MVC application

You need to share your website content with faceboo, twitter and google+ to increase traffic of your website. it's quite easy to implement. You need just a helper class and one line code in your page to call that helper class and done. so let's first write the helper class

public static class SocialNetworkingHelper
{
    #region[Twitter helper Method]   
    private static MvcHtmlString Social_Twitter(this HtmlHelper htmlHelper,
             String title, String url)
    {
        TagBuilder social_link = 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");
        social_link.Attributes.Add("data-url", url);
        return MvcHtmlString.Create(social_link.ToString(TagRenderMode.Normal));

    }
    #endregion

    #region[Facebook helper Method]
    private static MvcHtmlString Social_Facebook(this HtmlHelper htmlHelper, 
          String title, String url)
    {
        StringBuilder str = new StringBuilder();

        TagBuilder social_link = 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");
        social_link.Attributes.Add("data-href", url);

        str.Append(social_link.ToString(TagRenderMode.Normal));
        return MvcHtmlString.Create(str.ToString());
    }
    #endregion

    #region[Google plus helper Method]
    private static MvcHtmlString Social_GooglePlusOne(this HtmlHelper htmlHelper, 
        String title, String url)
    {
        StringBuilder str = new StringBuilder();

        TagBuilder social_link = new TagBuilder("div");
        social_link.Attributes.Add("class", "g-plusone");
        social_link.Attributes.Add("data-size", "medium");
        //social_link.Attributes.Add("data-size", "small");
        social_link.Attributes.Add("data-href", url);
        return MvcHtmlString.Create(social_link.ToString(TagRenderMode.Normal));
    }
    #endregion

    public static MvcHtmlString SocialLinkButtons(this HtmlHelper htmlHelper, 
       String title, String url)
    {
        StringBuilder str = new StringBuilder();
        TagBuilder ul= new TagBuilder("ul");
        ul.AddCssClass("social");
        ul.Attributes.Add("style", "list-style:none; width:100%;");

        /* -------- Facebook ------- */
        TagBuilder li1 = new TagBuilder("li");
        li1.InnerHtml = htmlHelper.Social_Facebook(title, url).ToHtmlString();
        li.AddCssClass("social-facebook");           

        /* -------- Twitter ------- */
        TagBuilder li2= new TagBuilder("li");
        li2.InnerHtml = htmlHelper.Social_Twitter(title, url).ToHtmlString();
        li2.AddCssClass("social-twitter");

        /* -------- Google --------*/
         TagBuilder li3= new TagBuilder("li");
         li3.InnerHtml = htmlHelper.Social_GooglePlusOne(title, url).ToHtmlString();
         li3.AddCssClass("social-google");            

         ul.InnerHtml = String.Format("{0}{1}{2}", li1, li2, li3); 
        str.Append(ul.ToString(TagRenderMode.Normal));
        return MvcHtmlString.Create(str.ToString());
    }
}

We have completed our helper class now we need to add these social networking buttons to page

<div>
   @Html.SocialLinkButtons(Model.Title, Request.Url.ToString())
</div>

Time to check output.

Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • mvc
  • c#
By Ali Adravi On 13 Jan, 13  Viewed: 3,420

Other blogs you may like

mvc search page example with code

MVC Searh page with pagination: It’s very easy to create a search page in asp.net but when I try to create the same in MVC I faced many problems, how to create model, how to keep searched values in search controls, pagination, I found myself nowhere, so start searching for some good examples but... By Ali Adravi   On 25 Aug 2013  Viewed: 36,927

MVC insert update delete and select records

CRUD (Create, Retrieve, Update and Delete) in MVC. When we start to learn new language, first we try to run an application with “Hello World” and then CRUD functionality. So in this article we will see how to select records from database (with WebGrid, pagination and sort functionality), update a... By Ali Adravi   On 17 Aug 2013  Viewed: 101,191

How to create a single thanks page for entire controller in MVC

Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or... By Hamden   On 30 Jun 2013  Viewed: 3,282

MVC jquery autocomplete with value and text field

In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the... By Ali Adravi   On 29 Jun 2013  Viewed: 6,457

Upload files with model data in MVC

Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi   On 04 Jun 2013  Viewed: 23,734