Resources » .NET programming » ASP.NET/Web Applications

How does the MVC architecture work


Posted Date: 17-Mar-2010  Last Updated:   Category: ASP.NET/Web Applications    
Author: Member Level: Silver    Points: 12


Asp.net MVC, MVC, Modal, View, Controller, Rest



In the early 2000 Microsoft came up with a new development framework called .net and along with that came this powerful IDE called visual studio. Both these together made a great impact on how web apps are built also, attracted so many thousands of developers to it. These two products have been evolving over the past 9years or so adding new items to their long list of features. ASP.NET mainly focueses on how to do things in an easier and shorter way. There have been four releases so far, .net1.0,.net1.1,.net2.0,.net3.5 and the latest being .net4.0.

The .net framework 4.0 includes a new web framework called ASP.NET MVC in addition to the usual ASP.NET Web forms.
I would like to dedicate this article to talk about MVC architecture in general to help everyone get an idea of what it is and how it behaves.

MVC stands for Model - View - Controller and these three are the building blocks that make up the MVC pattern. A website which uses MVC will be mainly based on the communication that happens between these three components.

What is a Controller?
Controllers will be the receivers and dispatchers. When a user sends a request by clicking a link or button then the request is handed over to the controller (There can be many controllers in the website and the correct controller to handle a particular request will be identified by the URL pattern) the controller will have a set of methods(show, index, etc...) defined and one of these methods will do the necessary tasks to generate a response. Basically, Controller will be the intermediary who will communicate with the Modal and generate an appropriate View and send that to the client.

What is a Model?
Model handles the database interactions for the application. The Model will return the data which is requested by the Controller.

What is a View?
View is the HTML output that a user sees when he makes a page request. This view will be rendered based on the Controllers' request and by the data that is passed in by the Model.

As explained earlier the MVC architecture defines a clear isolation between the Presentation layer and the Data access layer. The presentation will be in the View and the Data will be in the Model. These two never interact directly with each other. Just look at the steps below to get an idea of how a request/response cycle works in MVC, the numbers indicate the order of execution.

1.0. User request arrives
1.1. Controller receives the request
1.2. Pass it to the correct controller method
1.3. Fetch data from the database by initiating the appropriate Model
1.4. Pass the data from 1.3 to the View and generate the HTML View.
1.5. Send the generated HTML as the response.


Routing
Routing is the technique which is used to map the URL pattern to an appropriate Controller. Since, we don't make requests based on a page name(http://localhost/website/login.aspx) in MVC we need to tell our application the correct way to find the correct controller to handle the request, this process is called Routing. One should become good at this in order to have proper URLs in the website. Also, the URLs generated in MVC are SEO friendly. This is a significant feature to have cause we no longer need to implement our own dummy URL generator to handle SEO requests.

In conventional websites make requests to a page such as http://localhost/website/login.aspx and the server will look for this page within the website and the code which is written in that page will handle the request and an HTML output is presented to us. But, in MVC the URL would be like as follows http://localhost/website/products/1 this request will be passed to the Product Controller's show method which then passes a message to the Product model to fetch data with id=1 and then render an appropriate view to display the record fetched.

I hope that this article would have given you some sort of an idea on how MVC architecture works in generally. I also plan to write more in the future on MVC and various other topics and would like to share my experience with all of you. Please feel free to put your comments which would help me improve. Wanna get in touch then here is my email id: pragashonlink@gmail.com.


Did you like this resource? Share it with your friends and show your love!

Responses to "How does the MVC architecture work"
Author: Dhruv Patel    20 Mar 2010Member Level: Bronze   Points : 1
It is very very better to improve the knowledge and competition so can logic and quality of programmer is going to be increase....
Thanks...



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    arun
    sweta
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India