NuGet Package: Facebook helper in MVC Razor

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
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.
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 o your application.
    ViewBag.Title = "Home Page";

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

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.
Click Here
Is it helpful for you? Kindly let me know your comments / Questions.

1 comment:

  1. Hi I have a problem on remote server, on my local deploy everything works fine

    error CS0103: The name 'Facebook' does not exist in the current context
    System.Web.Compilation.AssemblyBuilder.Compile() at
    System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at
    virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean
    allowBuildInPrecompile, Boolean throwIfNotFound, Boolean