ThreadAbortException

May 2011 Entries

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!!

Using the MailDefinition class in .NET Framework 4 Console Application, Visual Studio 2010

One of my colleagues pinged me to check, how to use the MailDefinition class for creating formatted mails from a .NET Console Application.  She was referring to this article http://www.search-this.com/2009/02/05/aspnet-sending-email-both-in-html-and-plain-text/ which seems to be building a nice template email that can be sent. 

But this article was related to ASP.NET and hence didn’t have issues in referencing the MailDefinition Class which is part of System.Web.UI.WebControls namespace.

The application she was building was a Console Application.  However, it made sense that these namespaces weren’t available by default since a Console Application, by no means would require a System.Web.UI (related to UI) namespace unless there is a specific need.

First thing that came to my mind was, we should refer System.Web so that the subsequent namespace could be added.  When we checked adding Reference to System.Web from the Console Application, it didn’t show up.  The closest match we could find was System.Web.Services which wouldn’t be helping us in this case.

image

Then, I realized that by default, .NET Framework 4.0 based Console Applications/Windows Applications target the .NET Client Profile (I had written about it earlier http://geekswithblogs.net/ranganh/archive/2010/05/24/visual-studio-2010-winform-application-ndash-unable-to-resolve-custom.aspx)

So, we changed the Target Framework to .NET Framework 4 (instead of the default .NET Framework 4 Client Profile) and built the solution.    After that, in Add Reference, we could locate the System.Web namespace

image

After that, we could type MailDefinition in the Console Application, as below:-

MailDefinition message = new MailDefinition();

Note that MailDefinition still needs a reference to the specific System.Web.UI.WebControls namespace which it is part of.  But the good thing in Visual Studio 2010 is that, you can right click on a known class and Resolve it to automatically reference the assembly.

image

It would automatically add the appropriate reference in the using section.  In this case, it added the System.Web.UI.WebControls namespace.  After that it was all breeze and she could proceed with the MailDefinition class. 

Cheers !!!