This is 3rd post of my MVC series that discuss fundamental concepts of MVC with Jumpstart drill.
Model-View-Controller (MVC) is basically a software architecture pattern which comprises of 3 Tiers, each tier have different functionality. Controller is a main tier of MVC architecture that responsible for collecting request and responding to request. Model is a tier that responsible for validating data structure at database end as well as on the User Interface. While View is front end of MVC application in which data is presented to user.
Speaking of generally MVC does not have any version and can be implemented in any language. Since we are focussing on Microsoft technology therefore we will see different versions of MVC, which are versioned after updation of libraries.
Currently, in ASP.net MVC we have versions upto 4. We will be focussing on the latest that is i-e, MVC 4.
Some of the main Features that MVC 4 provides are:
1. Asp.net Web API.
2. New and Modernized default project template.
3. Supports for mobile application.
4. Mobile default project template.
5. Enhanced support for asynchronous projects and many more.
You can find detail list by following this link
Now we can go to real staff, creating our own First MVC 4 application.
Open you Visual Studio 2012 and Go to File > New Project
In New Project window select Web from left hand side menu under Template and Select MVC 4 Web Application from the list and name the project "MyFirstMVC4WebApp".
After pressing OK a new window will appear select a template as a Internet Application and make sure view engine is selected as a Razor.
If you wondering what about the rest of the template you select any of it and read descriptions that will tell you what other options do.
After Clicking OK. you will have a sample application with few codes appearing in the solution explorer. Run the Project to see how it looks like. For running Press F5 or Goto Debug > Start Debugging.
After running the application you will find basic layout app with navigation links home, about and contact.
Now stop the application by Shift + F5
Lets Create a demo page/view for ourself, to create a page/view, we will first go with the existing controller class. In Solution Explorer you will find controllers folder within your project, Expand it.
You will find 2 controllers classes that is already coded for you. Open HomeController.cs file.
In HomeController.cs you will find three methods these methods are usually known as "Action Methods". The main reason of calling these methods an action method because these are being called for every request, its an action of request.
In order to create a static view you need to create action method to respond with view. For creating action method, write the following code below the action method "Contact".
Now if you try to Run the application it will run without any errors. But when you navigate the action method that you have created, i-e, http://<localhost:port>/Home/MyFirstView/, it will give you ugly yellow and red error page.
This is because we have not created View at yet. To do so you need to go back to HomeController.cs file and just at end, where return statement is, you need to right click on 'View(); '.
Model-View-Controller (MVC) is basically a software architecture pattern which comprises of 3 Tiers, each tier have different functionality. Controller is a main tier of MVC architecture that responsible for collecting request and responding to request. Model is a tier that responsible for validating data structure at database end as well as on the User Interface. While View is front end of MVC application in which data is presented to user.
Speaking of generally MVC does not have any version and can be implemented in any language. Since we are focussing on Microsoft technology therefore we will see different versions of MVC, which are versioned after updation of libraries.
Currently, in ASP.net MVC we have versions upto 4. We will be focussing on the latest that is i-e, MVC 4.
Some of the main Features that MVC 4 provides are:
1. Asp.net Web API.
2. New and Modernized default project template.
3. Supports for mobile application.
4. Mobile default project template.
5. Enhanced support for asynchronous projects and many more.
You can find detail list by following this link
Now we can go to real staff, creating our own First MVC 4 application.
Open you Visual Studio 2012 and Go to File > New Project
In New Project window select Web from left hand side menu under Template and Select MVC 4 Web Application from the list and name the project "MyFirstMVC4WebApp".
After pressing OK a new window will appear select a template as a Internet Application and make sure view engine is selected as a Razor.
If you wondering what about the rest of the template you select any of it and read descriptions that will tell you what other options do.
After Clicking OK. you will have a sample application with few codes appearing in the solution explorer. Run the Project to see how it looks like. For running Press F5 or Goto Debug > Start Debugging.
After running the application you will find basic layout app with navigation links home, about and contact.
Now stop the application by Shift + F5
Lets Create a demo page/view for ourself, to create a page/view, we will first go with the existing controller class. In Solution Explorer you will find controllers folder within your project, Expand it.
You will find 2 controllers classes that is already coded for you. Open HomeController.cs file.
In HomeController.cs you will find three methods these methods are usually known as "Action Methods". The main reason of calling these methods an action method because these are being called for every request, its an action of request.
In order to create a static view you need to create action method to respond with view. For creating action method, write the following code below the action method "Contact".
Now if you try to Run the application it will run without any errors. But when you navigate the action method that you have created, i-e, http://<localhost:port>/Home/MyFirstView/, it will give you ugly yellow and red error page.
This is because we have not created View at yet. To do so you need to go back to HomeController.cs file and just at end, where return statement is, you need to right click on 'View(); '.
This will create view for you. to check the location of view, go to Solution Explorer, and expand View Folder. In View folder you will find sub folders, go to Home folder since our action method lies in HomeController you will find view with the name that we have provided during creation in this case it is same as Action Method name.
Thats it for today, In next post we will soon learn how to build CRUD applications and after that we will learn how to use Code First in our MVC application.
Further please feel free to comment or ask any question.
COMMENTS