asp.net MVC

How to share twitter post using asp.net MVC c#?

How to share twitter post using asp.net MVC c#?, someone asked me to explain?

The sample code in this article demonstrates share twitter post programmatically. To create a twitter application using Twitter apps and get a customer key (API Key) and customer secret (API key). Inorder to share twitter post we need to Install-Package TweetSharp using nuget package manager console and reference to the page.

if you want to share image via twitter refer this link

Import following namespaces,

using System.IO;
using TweetSharp;

Step 1: Tools ->nuget package manager->package manager console->

PM> Install-Package TweetSharp

Step 2: Right click on the "Controllers" folder and add "Twitter" controller. Copy and paste the following code. Please make sure to include "MVC_tutorials.Models" namespace.

  private void sharet_witter()
        {
                var oauth_consumer_key = "your API key";
                var oauth_consumer_secret = "Your API secret key";
                string token = "access token";
                string tokenSecret = "access token secret ";
                StringBuilder str = new StringBuilder();
                str.AppendLine("hello word");
                var service = new TweetSharp.TwitterService(oauth_consumer_key, oauth_consumer_secret);
                service.AuthenticateWith(token, tokenSecret);
                service.SendTweet(new SendTweetOptions
                {
                    Status = str.ToString()
                });
                TempData["SuccessMessage"] = "tweeted success";
        } 

Step 3: Right click on the "Index" action method in the "TwitterController" and add "Index" view. Copy and paste the following code.

<style type="text/css">
    .btn {
        font-size: 19px;
        width: 100px;
        height: 45px;
        background: #00BCD4;
        border-style: solid;
        border-color: white;
        color: white;
    }
</style>
  @if (TempData["SuccessMessage"] != null)
                        {
                            <div class="errorBlock" style="display: block; padding-left: 65px; color: black; border-color: #f0c36d; background-color: #f9edbe; font-size: 16px">
                                @TempData["SuccessMessage"]
                            </div>
                        }
  @Html.ActionLink("share twitter", "share_twitter", new { Class = "btn"})

Output:

share twitter post using in asp.net c sharp MVC

Post your comments / questions