Creating and Deploying an ASP.NET MVC4 Application

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.

MVC1.jpg

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:

MVC2.jpg

When you hit "Ok" then you will be presented with many of options, as in:

MVC3.jpg

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:

MVC4.jpg

Now run the application and see the URL to "\Home\Index" where it came from; see:

MVC5.jpg

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:

MVC6.jpg

"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:

MVC7.jpg

Creating a Model in MVC

Right-click on the Model class and add a class, as in the following:

MVC8.jpg

Now add some properties to your model as in the following:

MVC9.jpg

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:

MVC10.jpg

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.

MVC11.jpg

Add the initial migration as below:

MVC12.jpg

MVC13.jpg

The code inside the initial file is as below:

MVC14.jpg

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:

MVC15.jpg

Deploy the site to Windows Azure

Now navigate to manage.windowsazure.com and click "Custom create".

MVC16.jpg

MVC17.jpg

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:

MVC18.jpg
Source: http://www.techbubbles.com/aspnet/creating-and-deploying-an-asp-net-mvc4-application/


Similar Articles