Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

NuGet Package: Facebook helper in MVC Razor

, 23 Jul 2012
Rate this:
Please Sign up or sign in to vote.
The era of global connectivity has begun. Social networking being the first step in this process, most of the latest technologies are coming up with different kinds of support for social network sites.

The era of global connectivity has begun. Social networking being the first step in this process, most of the latest technologies are coming up with different kinds of support for social network sites.
One of the famous of such social networks is Facebook.

Recognized this button. Yes, in this post we will see how to implement this kind of functionality in your MVC Razor application with a single line of code. Even Microsoft recognized the importance of social networking and thus introduced different helper classes for different social network sites.

Step 1: Create a MVC Razor application. Go to references and right click and select "Nuget Package Manager".

NuGet Plugin/Package Manager will appear.

Step 2: Search with Facebook in Nuget Manager Console. Select Facebook.Helper Nuget package.

Click on install button. Once it is installed, a green color tick mark will appear saying this Package is installed on your machine. Restart Visual Studio for after effects.

Step 3: It is very important to know what happened because of step 2. For that reopen the project and see that you will have a "App_Code" folder newly created in your solution. Open it and see, you will have a "Facebook.Cshtml" file.

It will have all the Helper methods which will be used to handle Facebook infrastructure in your application.

Step 4: Now go to your Index Page, add the below line

@Facebook.LikeButton()

There are many other helper methods available with Facebook Helper class created. But for time being we test only Like Button. Now execute the application and see the result.

Result:

Step 5: Before going to see why we got this error, we need to know about two DLLs. WebMatrix.Data.dll : Provides methods and properties that are used to access and manage data that is stored in a database.

WebMatrix.WebData.dll : Provides security and authentication features for ASP.NET Web Pages applications, including the ability to create user accounts, log users in and out, reset or change passwords, and perform related tasks.

Now, from significance of these two dlls, you will understand that the Facebook helper class need the support of these two DLLs to log in the user and get/post the data to Facebook application. The above error is due to lack of these two dlls.

Step 6: Now reffer these DLLs and mark them copy local true. Reason being, these dlls need to be in local bin folder, to make Facebook helper infrastructure work.

Step 7: Now before execution, let me tell you how Facebook Login work. If you are trying to access Facebook for the first time, Facebook will ask you to authenticate. Once after authentication, the credentials will be saved as a cookie on your machine. So, next time when you hit Facebook, it will check for that cookie, and if present, it will read the account details from cookie and take you directly to home page without authentication.

Step 8: Execute and see out put to your application.

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

<h2>@ViewBag.Message</h2>
<p>
<p>
        <h5>This si a dumy Paragraph Created to diplay some text on this page.</h5>
                <b>New York:</b>  An Indian diplomat's daughter, who was jailed for a day on suspicion of sending obscene emails to her teacher, has filed a lawsuit against the city of New York and her school authorities seeking USD 1.5 million in damages for her wrongful imprisonment and suspension from school.
 Krittika Biswas, 18, the daughter of Vice-Consul in the Consulate General of India in New York Debashish Biswas, was detained and arrested in February last year on the grounds that she had sent "offensive and sexually threatening" emails to her teachers in Queens's John Browne High School.    
</p>
@Facebook.LikeButton()  
</p>

Out Put:

When Loaded

When i click on Like button.

When i am posting the comment on current webpage.

 Finally How it resulted in Facebook.

With this we have achieved the objective of adding Facebook infrastructure using Facebook helper class in MVC Razor application. Code: Click Here
Is it helpful for you? Kindly let me know your comments / Questions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
QuestionFacebook.Helper with mvc 5 PinmemberZainNabi19-Aug-14 1:39 
QuestionFacebook.Helper with mvc 5 PinmemberZainNabi18-Aug-14 10:12 
QuestionHello PinmemberMember 995916110-Apr-13 20:07 
Questionhello PinmemberMember 995916110-Apr-13 20:06 
QuestionHttpCompileException Pinmembervitalisfan23-Jul-12 21:33 
QuestionThanks PinmemberPatrick Harris23-Jul-12 16:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 23 Jul 2012
Article Copyright 2012 by PratapReddyP
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid