TechBubbles Microsoft Technology BLOG

Creating and Deploying an ASP.NET MVC4 Application

This post gives you an introduction to MVC model and outlines the steps in 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 MVC Works?

image

User sends a request to your web site and that request through a feature called routing goes to controller, In MVC URL does not maps to a file , it is just Controller action just a method which 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 points in MVC are logic in the controller and data is in Model and presentation in the View. 

1. Create a new project using Visual Studio 2012 and select MVC4 web application from dialogue box

image

When you hit ok then you will be presented with a lot of options

image

If you look at your solution explorer then you will find a folder for controller, folder for model and one for views

image

Now run the application and see the URL \Home\Index where does it came from

image

It is a Home controller and Index method, so if some body browses Home it then uses the Home controller and if they type index then it means it uses index action method

image

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 it is then Home is default controller and view index method

image

Creating a Model in MVC

right click on Model class and Add a class

image

Now add some properties to your model as shown below

image

build the model and right click on the controller folder and say Add controller now give a name to controller and select the model from the drop-down and create a new database context as shown in below picture

image

It now adds all necessary controller and views methods to the person. Now enable migrations on your project. Migrations is 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 database. Using Package-Manger Console you can enable migrations and after that it creates a migrations folder in solution explorer.

image 

Add the initial migration as below

image

image

The code inside initial file as below

image

Now call update-database command in package-manager console to update with those changes in the code. Now run the application and type person in the URL

image

Deploy the site to Windows Azure

Now navigate to manage.windowsazure.com  and click custom create

image 

image

create a database and then link that to your deployed web site. Download the publishing profile settings file from this portal and import into Visual Studio publish process

image

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud