Create Facebook App using ASP.NET MVC

Introduction

In this article I show how to create an app on Facebook that will be connected to your ASP.NET MVC Project.

In this article we will use the MVC 4 template in Visual Studio 2012 to create a Facebook App. The app is created at the end of this article will look something like this:

fb019.jpg

Now let's start creating the App.

Step 1

First of all you need to create a New Web Project in MVC 4, for that:

  1. Run your Visual Studio 2012.
  2. Select "File" -> "New" -> "Project...".
  3. Now under the Templates first expand the C# node then the Web node then select "ASP.NET MVC 4 Web Application".
  4. Now provide the name for this project and then click on the "OK" Button.

fb12.jpg

On the next page you need to select the "Facebook Application Template", this is a very necessary step since this is the template that will help to create the Facebook App. Remember that the Facebook template will be available under the "Razor Engine" only.

fb13.jpg

Step 2

Now click on the name of your project and then press the F4 Key, this will open the properties of your project or you can open it from the File menu.

fb14.jpg

On pressing the F4 key the "Properties Section" will be opened, here you need to do two things:

  1. First thing is to make SSL Enabled by making it True.
  2. Second is to copy the SSL URL provided below the SSL Enabled.

fb15.jpg

Step 3

Now right-click on your project in the Solution Explorer and select the Properties.

fb16.jpg

In the Properties select the "Web".

fb17.jpg

In the Web, paste the copied SSL URL in the Project URL and then click on "Create Virtual Directory".

This will create the Virtual Directory for this URL, this URL will be used to configure Facebook.

fb18.jpg

Step 4

Now open your browser and open this link: https://developers.facebook.com/apps

Now on this page use the following procedure:

  1. Login with your Facebook ID.
  2. If you are not Registered as a Developer then click on "Register as Developer", then provide all the requested information.
  3. After you have registered you will find a button named "+ Create New App" on the default page, click on this button.

fb1.jpg

Now you need to provide an App Name  and an App Namespace for this app. If you don't provide the namespace for the app then the App Id will be used in the URL Section. This namespace will be part of the URL and will be used to access your Facebook Application; for example "https://apps.facebook.com/clbbday." After providing the name and namespace click on "Continue" Button.

fb2.jpg

Step 5

Now on the Basic Setting page set the Sandbox Mode to Enabled, this will let you to access your app and modify it but it will restrict your app from being used by others until you allow everyone to access it.

On this page you can also see that the "App ID" and "App Secret" is also provided, these will be used in the next step of this article.

fb3.jpg

Now open the Web.Config file of your project, in the Web.Config you will find that space for the App ID, App Secret and App Namespace is provided.

Just copy these things from the basic page and paste in the Web.Config file.

fb4.jpg

Step 6

Now again go to the Properties and click on Web, from here copy the Project URL.

fb5.jpg

Now go to the Basic Page and paste this URL into the Canvas URL and in the Secure Canvas URL.

You also need to change the Canvas Width to Fluid, this will help your app to change it's size depending on the browser's size.

After providing these things click on the "Save Changes" button.

fb6.jpg


fb7.jpg

Step 7

Now go back to your project and press "Ctrl+F5" to run the application or debug it.

Now your browser will prevent you from accessing it and will give you a Security Certificate Warning because IIS Certificates are not trusted by the browser, so click on "Proceed Anyway" to use the app.

fb8.jpg

Now your app will be in a running state and it will look like this:

fb019.jpg


Note: It might happen that when you try to find the Facebook Application Template under your MVC 4 Project, you will get to know that it's not available there in that case you need to download the latest ASP.NET and Web Tools, for that just click on this link :- http://www.asp.net/vnext/overview/latest