ASP.Net MVC 3 - An Overview Part 1
ASP.Net MVC 3 is one of the approaches of developing Web Applications using Microsoft Technology.The other approach is using ASP.Net Web Forms which is built on .Net framework.Depending upon the architectural decision we need to choose which approach will best suit with our requirement.Each approaches have its own prons and cons like any other technology.We Explore them in detail in this section.
Welcome to ASP.Net MVC What is ASP.Net MVC 3?
ASP.Net MVC 3 is a microsoft framework for developing web applications.A framework is nothing but structural piece of software upon which other applications are built.Since its a framework it will automate all the tedious and common task so that development will be easier.This will make life of a progammer quite easy.Moreover it is an open source software. Journey of ASP.Net MVC
1. ASP.Net MVC 1 was released on March 2009.
2. ASP.Net MVC 2 was released on March 2010.
3. ASP.Net MVC 3 was released on January 2011.
4. ASP.Net MVC 4 beta released on February 2012.
Out of these, currently ASP.Net MVC 3 is available for testing.
MVC Pattern Vs ASP.Net MVC 3
They are not interchangeable.Here MVC refers to Model-View-Controller.Its quite older design pattern.As you know,patterns formalizes best pracices. The only relation between ASP.Net MVC 3 and MVC is that ASP.Net MVC 3 framework uses MVC pattern as their architetural pattern. ASP.Net Web Forms Vs ASP.Net MVC 3
I already mentioned both ASP.Net Web Foms and ASP.Net MVC 3 are different approaches developing web applications.Both are built on top of ASP.Net.Both does not hide stateless nature of web development.But there exists subtle differnces between these two approaches.Few of them are listed here:
1. ASP.Net Web forms built on Page Contoller Pattern where we add functionalty to individual pages where as ASP.Net MVC 3 is build on Model-View-Controller Pattern.Here controller is the core object who manages to render the appropriate view.
2. ASP.Net MVC 3 is designed in such a way that it make unit testing very efficient where in case of ASP.Net Web forms unit testing is quite tedious.
3. ASP.Net MVC 3 does not use postback,viewstate,no page lifecycle,does not have rich server controls,etc,but it provides full control of the rendered HTML.Where as ASP.Net Web forms supports postback,view state,rich controls etc.,but here we don't have full control over the rendered HTML.
4. ASP.Net MVC 3 follows Test Driven Application Development(TDD) where as ASP.Net Web forms not.
5. ASP.Net Web forms provides Rapid Application Development(RAD)where as
ASP.Net MVC 3 provides loosely coupled approach where it follows seperation of task. Main Ingredients of ASP.Net MVC
The main parts of ASp.Net MVC are:
1. Model : A set of classes which contains the application data and the business logic for how data can be inserted or updated or deleted.The model is accessible by both controller and view.By the help of model, we can keep data object and the logic that operates on the data seperate from rest of the application.
2. View : Responsible for displaying the User Interface.
3. Controller :It is the heart of MVC which is responsible for handling communication from the user and it contains application specific logic. As it name implies it controlls the entire application.Controller can access model class to pass data to the view. Features of ASP.Net MVC
1. seperation of application task in to model,view and contoller.Thus it helps to manage complexity.Also multiple developers can independently work for the same module.
2. It does not use viewstate,postback,web forms and server controls and does not have page life cycle.so it gives developers full control over the rendered HTML.
3. It follows a test driven development approach.
4. Unit testing the application is very easy and efficient compared to its web form counter part.
5. Easy integration with Javascript. What is new in ASP.Net MVC 3
Listed are few important features of ASP.Net MVC 3.
1. Razor view Engine
2. JQuery Validation Plugin.
3. Improved Ajax Support.
4. Nuget
5. Global Action filters
6. Dynamic language support. Requirements for ASP.Net MVC development
Operating System : Window XP,Vista,2003,2008
Softwares :
1. Visual Studio 2010 0r web developer 2010
2. ASP.Net MVC 3 Tools Steps for developing ASP.Net MVC 3 Application
1. Open Visual Studio 2010 0r web developer 2010
2. Select ASp.Net MVC 3 Application
3. Select the application template,view engine and check the unit test project checkbox if you wish to geneate testcases. About Application Templates
There 3 types of templates available.They are:
1.Internet Application template : This is meant for Web Application
2.Intranet Application template : This is meant for Windows Application
3.Empty template : This is meant for experienced MVC developers who wish to do set up and configuration according to their needs. About View Engine
View Engine allows to generate HTML mark up in the ASP.Net application. There are 2 types of view engines are available.They are:
1. ASPX view : This is the default view. Till MVC 2 only ASPX view were available. It uses comples xml like syntax.
2. Razor view : This is a new feature available in MVC 3.It uses raizor's syntax. Files and Directories
If you choosen any template other than empty template some files and directories are added to the ASP.Net MVC project automatically.They are :
1. Controllers : This folder contains the controller classes which handle the URL request.
2. Views: This folder contains the view responsible for rendering the HTML.This contains subfolder called Shared which contains all the reusable components.
3. Models : This folder contains classes that repesent and manipulate data or business objects.
4. Script : This folder contains Javascript files.
5. Content : This folder contais CSS and image files.
6. App_Data : This folder contains data files that you want to read/write.
7. Global.asax
8. Web.Config
9. Packages.config etc. What is Controller ?
In ASP.Net MVC,controller is a class inherited from System.Web. Mvc. Contoller class. All the incoming request in an ASP.Net application are handled by contollers.It is responsible for flow control logic.Once the request comes to the controller it will communicate with the model and once it got a response from the model it is the contoller who decides which view should be rendered. Sample Controller Class
public class ProductController: Controller {
public Actionesult index(){
return View();
}
}
Here index is the controller's action method and action result is a class which allows to returns a view. I will dicuss more about action result later. What is View ?
You can see in the above example, that a controller action return a view.A view contains HTML mark up and content that is sent to the browser.View must be created in the right location under the view folder.
Rules for creating a view :
1. A sub folder must be created under the Views folder with the same name as your respective controller.
2. Within the above mentioned sub folder create .cshtml/.vbhtml file in case of razor view and .aspx file in the case of ASPX view.
3.The name of the above file must be same as the controller action. Sample View
@{
ViewBag.Title = "Index Page";
}
Here @{...} is the razor syntax.In prior view engines if you can recollect we have used <%...%> syntax. This is one among the many new features of ASP.Net MVC 3. What is a Model ?
Model is a class which contains application business logic,validation logic and data access logic. ASP.Net MVC 3 Applicaton Life Cycle
The following are major stages in the life of ASp.Net MVC application.
1. User enters a specific URL in the browser: At this time,in the Global.asax file,route objects are added to the route table collection.
2. Perform routing : The Url routing module uses the first matching route object from the route table collection.
3. Create MVC request handler: The MvcRouteHandler object creates an instance of MvcHandler class and passes the RequestContext instance to the handler.
4. Create Controller: The Mvcandler object user the RequestContext instance to identify the IControllerFactoy object.
5. Execute Controller: The Mvcandler instance call the controller's execute method.
6. Invoke Action : The ControllerActionInvoker object associated with the contoller determines which action method of the controller to call and call the respective method.
7. Execute result: The controller action method communicate with the model to get the response data and execute the result by returning the result type.
ASP.Net MVC framework handle all these actions in the back ground.So the developer does not bother about these knitty-gritty details.
A Good Article presented in an easy understandable way