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

NuGet Package: Facebook helper in MVC Razor

By , 23 Jul 2012
 

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)

About the Author

PratapReddyP
Web Developer
India India
Member
I am an Aspiring Architect, having expertise in Design and Development. Good knowledge on TDD and DDD Approaches. Well versed with Agile methodology. Strong at Sharepoint, C# , Asp.Net, MVC, MVC Razor, WCF, Entity Framework, SQL, BI Tools.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHellomemberMember 995916110 Apr '13 - 20:07 
Is there an example you can download?
QuestionhellomemberMember 995916110 Apr '13 - 20:06 
請問有範例可以下載~
QuestionHttpCompileExceptionmembervitalisfan23 Jul '12 - 21:33 
Everything works fine (on my local iis after deploy). But at my remote server i get an error. Please help
 

at System.Web.Compilation.AssemblyBuilder.Compile() at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtual
 
Error:
d:\path...\wwwroot\views\shared\_layout.cshtml(45): error CS0234: The type or namespace name 'Facebook' does not exist in the namespace 'ASP' (are you missing an assembly reference?) at System.Web.Compilation.AssemblyBuilder.Compile() at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean
QuestionThanksmemberPatrick Harris23 Jul '12 - 16:09 
I need to implement this on our website.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 23 Jul 2012
Article Copyright 2012 by PratapReddyP
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid