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 to your application.
ViewBag.Title = "Home Page";
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.
Is it helpful for you? Kindly let me know your comments / Questions.