Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Much has been talked about the new release of ASP.NET MVC, i.e. MVC 3 and the new Razor syntax and the ability to simplify the development.  Razor uses the @ prefix for switching between code and HTML and that kind of simplifies it when compared to using <%: symbol as with the case in MVC and ASPX engines.

Here is a nice article from David Ebbo on how all of these fit together

Not just that, Razor comes with a lot of goodies.  The Helper classes simplify in doing some of the common tasks, doing common scenarios like integrating Twitter, Facebook or PayPal into your applications.

To begin with, am using the following

1. Visual Studio 2010 Service Pack 1.  You can install it from Download details- Microsoft Visual Studio 2010 Service Pack 1 

2. ASP.NET MVC 3 and the Tools Update.  You can install it from http://www.microsoft.com/downloads/en/details.aspx?FamilyID=82cbd599-d29a-43e3-b78b-0f863d22811a&displaylang=en

Once, everything is set, create a File – New – ASP.NET MVC3 Web Application and give it a name “TwitterHelperDemo”

image

In the next screen, allow the default “Razor” as the View engine and click ok to create the web application

image

 

Visual Studio creates the skeleton project structure and scaffolds the default Home Controller and Account Controller.  For the purpose of this sample, we will just use the Index.cshtml View which is under the Home folder under Views.

Now, we need to get the Helpers installed before starting to use.  In the Visual Studio Solution Explorer, right click the Project and select “Add Library Package Reference”

image

The “Add Library Package Reference” dialog opens up and lists the installed Helpers. 

Click on the “Online” tab and type “Twitter” in the search box on top right.  It will try and find results online.  Choose the Twitter.Helper (click) and Install.

image

It installs in a second and you can close the dialog.

In the root director, you will find packages.config file.  Open the file to verify if the package is installed.  If it is installed, it would list as

<package id="Twitter.Helper" version="1.0" />

Next, open the Index.chstml file and remove the default “To Learn ASP.NET MVC” message within the paragraph. 

Start typing @Twitt and you should see intellisense for TwitterGoodies.

Add @TwitterGoodies.Profile(“TWITTER HANDLE”)

When you run the solution, you can see the Twitter Profile loading and the latest tweets showing up.

image

You can try more options like @TwitterGoodies.Search("#webcamps") and @TwitterGoodies.TweetButton()

The complete code for this page is

@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
   @TwitterGoodies.Profile("ranganh")
</p>
<p>
@TwitterGoodies.Search("#webcamps")
</p>
<p>
@TwitterGoodies.TweetButton()
</p>

There are even more options like FollowButton, WriteTo which expects a TextWriter to post your tweet from the page etc.,

This way, we can simply integrate Twitter into your ASP.NET MVC 3 Application.

Cheers!!

Print | posted on Tuesday, May 24, 2011 11:06 AM

Comments on this post

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
I was trying to use this with Silverlight, but not getting the reference. Does it has any limitation that it will work only with ASP.NET MVC and Razor ?
Left by Brij Mohan on May 24, 2011 5:41 PM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
Very cool!
Left by Gregor Suttie on May 26, 2011 2:28 AM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
Great one.. thanks
Left by nithin mohan on May 30, 2011 7:09 AM

# Mr

Requesting Gravatar...
Good to know it... thanks for the tips.. but I also have the same question added before.. does it have any limitation??
Left by Web Designers Ryde on Jun 13, 2011 5:37 PM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
Can you post something similar for FaceBook. FB has many limitations - hope they did that intentionally for developers to customize
Left by Shasur on Sep 20, 2011 6:25 PM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
Microsoft has intrduced twitter control in ajax control toolkit in asp.net 4.0, september release 11.

Please visit for more detail

http://www.dotnetlogix.com/Article/Ajax/188/Introduction-to-Twitter-Control-in-Ajax-Control-Toolkit.html

Thanks
Left by Najmul Hoda on Oct 19, 2011 2:49 PM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
This is awesome! Thanks man.
Left by Paul on Jun 11, 2012 7:29 PM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
Hey, great post.

I followed each step, but i am not getting my profile. My browser open the index page and the page do not show my profile. Please help me.
Left by Nimit Joshi on Sep 10, 2013 6:18 PM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
I have tried intergrating both the apps but each time I try i face error message pops up .I want the problem to be fixed soon. Please help me. I have downloaded the new version of ASP.NET and I hope it works now.
lake erie fishing charters
Left by pine on May 29, 2014 5:36 PM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
I have faced Problem to view my profile i have working in mvc5 and when the page loaded i found no profile .the page show only button of post.please reply
Left by farhad Abbas on Oct 07, 2015 11:58 AM

# re: Integrating Twitter with your ASP.NET MVC 3 and Razor Web Application

Requesting Gravatar...
plz help me about twitter integration project
Left by traghuwanshi on Jan 04, 2016 6:27 PM

Your comment:

 (will show your gravatar)