This article provides an introduction to the MVC model and outlines the procedure for creating a new site and adding a model, controller and view using Entity Framework Code First. It also gives you the steps to deploy this site to Windows Azure Web Sites.
How does MVC Work?
The user sends a request to your web site and that request, through a feature called routing goes to controller, in the MVC URL does not map to a file, it is just a Controller action, just a method that does some stuff and returns data, this data can be a customer object or a string of text. Once the controller gets this data and it passes it on in the form of Model to a View. The Job of the View is to take the Model and display it. The important point in MVC is the logic in the controller and data is in the Model and the Presentation is in the View.
1. Create a new project using Visual Studio 2012 and select "MVC 4 Web Application" from the dialog box; see:
When you hit "Ok" then you will be presented with many of options, as in:
To understand the purpose of each application template in the dialog above read this article.
If you look at your Solution Explorer then you will find a folder for the controller, a folder for the model and one for the views; see:
Now run the application and see the URL to "\Home\Index" where it came from; see:
It is a Home controller and Index method, so if somebody browses to Home it then uses the Home controller and if they enter "index" then it means it uses the index action method. See:
"Return View" goes to your Views folder and then to Home and then to index. If you do not specify a controller in your MVC application then Home is the default controller and view index method. See:
Creating a Model in MVC
Right-click on the Model class and add a class, as in the following:
Now add some properties to your model as in the following:
Build the model and right-click on the controller folder and say Add controller. Now provide a name to the controller and select the model from the drop-down and create a new database context as shown in following picture:
It now adds all necessary controller and views methods to the person. Now enable migrations on your project. Migrations is an Entity Framework feature, it allows you to manage the evolution of model and the database. Using these migrations you can also mention your changes in the model back to the database. Using the Package-Manager Console you can enable migrations and then it creates a migrations folder in Solution Explorer.
Add the initial migration as below:
The code inside the initial file is as below:
Now call the update-database command in the package-manager console to update with those changes in the code. Now run the application and type in the person in the URL. See:
Deploy the site to Windows Azure
Now navigate to manage.windowsazure.com and click "Custom create".
Create a database and then link that to your deployed web site. Download the publishing profile settings file from this portal and import it into Visual Studio publish process as in the following: