Click here to Skip to main content
Click here to Skip to main content

Understanding ASP.NET MVC (Model View Controller) Architecture for Beginners

By , 14 May 2012
 

Introduction

This article is intended to provide basic concepts and fundamentals of ASP.NET MVC (Model View Controller) architecture workflow for beginners.

“M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner than the traditional ASP.NET web development. Web applications developed with ASP.NET MVC are even more SEO (Search Engine) friendly.

Developing ASP.NET MVC application requires Microsoft .NET Framework 3.5 or higher.

MVC Interaction with Browser

Like a normal web server interaction, MVC application also accepts requests and responds to the web browser in the same way.

Inside MVC Architecture

The entire ASP.NET MVC architecture is based on Microsoft .NET Framework 3.5 and in addition uses LINQ to SQL Server.

What is a Model?

  1. MVC model is basically a C# or VB.NET class
  2. A model is accessible by both controller and view
  3. A model can be used to pass data from Controller to view
  4. A view can use model to display data in page.

What is a View?

  1. View is an ASPX page without having a code behind file
  2. All page specific HTML generation and formatting can be done inside view
  3. One can use Inline code (server tags ) to develop dynamic pages
  4. A request to view (ASPX page) can be made only from a controller’s action method

What is a Controller?

  1. Controller is basically a C# or VB.NET class which inherits system.mvc.controller
  2. Controller is a heart of the entire MVC architecture
  3. Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views.
  4. Controller can access and use model class to pass data to views
  5. Controller uses ViewData to pass any data to view

MVC File Structure & File Naming Standards

MVC uses a standard directory structure and file naming standards which are a very important part of MVC application development.

Inside the ROOT directory of the application, there must be 3 directories each for model, view and Controller.

Apart from 3 directories, there must have a Global.asax file in root folder, and a web.config like a traditional ASP.NET application.

  • Root [directory]
    • Controller [directory]
      • Controller CS files
    • Models [directory]
      • Model CS files
    • Views [directory]
      • View aspx/ascx files
    • Global.asax
    • Web.config

ASP.NET MVC Execution Life Cycle

Here is how MVC architecture executes the requests to browser and objects interactions with each other.

A step by step process is explained below [Refer to the figure as given below]:

Browser Request (Step 1)

Browser request happens with a specific URL. Let’s assume that the user enters URL like: [xyz.com]/home/index/

Job of Global.asax – MVC routing (Step 2)

The specified URL will first get parsed via application_start() method inside Global.asax file. From the requested URL, it will parse the Controller, Action and ID.

So for [xyz.com]/home/index/:

  • Controller = home
  • Action = index()
  • ID = empty — we have not specified ID in [xyz.com]/home/index/, so it will consider as empty string

Controller and Action methods (Step 3)

MVC now finds the home controller class in controller directory. A controller class contains different action methods,

There can be more than one action method, but MVC will only invoke the action method which has been parsed from the URL, its index() in our case.

So something like: homeController.index() will happen inside MVC controller class.

Invoking action method can return plain text string OR rendered HTML by using view.

Call to View (Step 4)

Invoking view will return view(). A call to view will access the particular ASPX page inside the view directory and generate the rendered HTML from the ASPX and will respond back to the browser.

In our case, controller was home and action was index(). So calling view() will return a rendered HTML from the ASPX page located at /views/home/index.aspx.

This is it, the whole process ends here. So this is how MVC architecture works.

History

  • 26th January, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

kiran dangar
Software Developer (Senior) Gateway Technolabs Pvt. Ltd
India India
Having 4+ years of Technical Experience in asp.net,c#.net, SQL Server 2008,AJAX, XML,JQuery, JavaScript, .net framework, WCF, WPF/Silverlight,SSIS, SSRS, asp.net MVC.
 
While not working loves Photography and bike riding. Playing computer games are the best stress buster for him Smile | :)
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4 PinmemberRajeshKumar Sugumar15-Apr-13 20:51 
Nice and Easy to Understand for Beginners like me
QuestionExcellent article PinmemberDalePres28-Mar-13 5:52 
I think the article is great.   For more information, get a book or MSDN site.   For a highlevel overview of the architecture (isn't architecture supposed to be high-level anyway) this is good.
GeneralMy vote of 3 PinmemberGeorge L. Jackson12-Mar-13 4:06 
This is more of an outline of an article. More discussion on presented topics would have been very benificial. The ASP.NET Execution Life Cycle figure was not legible.
GeneralMy vote of 2 PinmemberEnrique Albert4-Mar-13 22:57 
Poor in terms of contents and examples
SuggestionASP.net MVC Interview Questions Answers Pinmembermitu sinha12-Jan-13 6:07 
Hi,
visit for ASP.Net MVC interview Question answers
ASP.net MVC Interview Questions Answers[^]
QuestionMy vote of 4 Pinmembermagiccoder1-Jan-13 23:56 
My vote of 4
GeneralMy vote of 2 PinmemberMember-394866314-Nov-12 22:51 
Photos are not loading from anywhere (home, office, friend's home). Checked on chrome and firefox.
GeneralRe: My vote of 2 Pinmemberrohan garud10-May-13 23:21 
GeneralMy vote of 3 Pinmemberalexalexalex190723-Oct-12 4:41 
there is no example enough
Questionits good Pinmemberwaseem-ricky29-Sep-12 21:04 
its good but i need more information or more clarity about MVC...
GeneralMy vote of 4 PinmemberPravin028-Sep-12 3:10 
good one
GeneralGreat Description Pinmembergabapradeep15-Sep-12 22:11 
Hi,
The post is great. Smile | :)
QuestionSome error on the directory name? Pinmemberirnbru12-Sep-12 23:56 
It should be "Controllers" instead of Controller. I was wondering why the plurial was missing.
AnswerRe: Some error on the directory name? Pinmemberkiran dangar13-Sep-12 19:18 
Questionthanks Pinmemberdeepak3dart12-Sep-12 19:12 
thnaks....cheer
GeneralMy vote of 4 PinmemberFaithcz13-Aug-12 3:22 
Helpful article, but could contain more information.
GeneralMy vote of 3 PinmemberRGhaywat2-Jul-12 19:37 
good but very short
GeneralMy vote of 4 Pinmembercrcklssp5-Jun-12 19:43 
Nice one to start with.
GeneralRe: My vote of 4 Pinmemberkiran dangar6-Jun-12 3:08 
GeneralMy vote of 5 PinmemberMuhammad Hussain Tabassum28-May-12 2:33 
excellent!
GeneralRe: My vote of 5 Pinmemberkiran dangar30-May-12 3:12 
GeneralMy vote of 5 Pinmembermeeram39524-May-12 7:02 
Good.Nice explanation for beginners Smile | :)
GeneralRe: My vote of 5 Pinmemberkiran dangar30-May-12 3:13 
GeneralMy vote of 3 Pinmemberamitgajjar23-May-12 19:30 
it would be great if you explain with an example.
GeneralMy vote of 3 PinmemberLFD-Mike23-May-12 6:03 
As previously mentioned, the interaction with the model is peculiarly absent. As presented there is no data being used, which would put the MVC in a pure overhead role.
The only thing that hints of data is Linq To Sql in an image, which for the most part is an orphaned technology.
GeneralMy vote of 5 Pinmembermanoj kumar choubey16-May-12 21:23 
Nice
GeneralRe: My vote of 5 Pinmemberkiran dangar16-May-12 22:21 
GeneralRe: My vote of 5 PinmemberMadhaviLatha.Bathini21-Apr-13 1:14 
GeneralMy vote of 5 Pinmemberlavikgupta8-May-12 23:32 
Image of MVC Execution Life Cycle is very blur.Plz make it visible.
GeneralRe: My vote of 5 Pinmemberkiran dangar14-May-12 1:23 
GeneralMy vote of 4 PinmemberVarun Sareen19-Apr-12 19:07 
An example here with the article would be much better to explain the concept of MVC
GeneralRe: My vote of 4 Pinmemberkiran dangar30-Apr-12 19:08 
GeneralMy vote of 3 PinmemberShanmugam R P2-Apr-12 6:01 
Good
GeneralRe: My vote of 3 Pinmemberkiran dangar30-Apr-12 19:08 
SuggestionMy vote of 4 PinmemberSridhar Patnayak23-Jan-12 6:02 
Good article, but if you explain it with a simple example, it will be good.
GeneralRe: My vote of 4 Pinmemberkiran dangar2-Feb-12 0:45 
SuggestionImages not held locally PinmentorDaveAuld1-Jan-12 5:22 
Hi,
 
Could you please edit your article to ensure that the images are held locally on CodeProject and not linked out to wordpress.
 
Thanks,
Dave
Find Me On: Web|Facebook|Twitter|LinkedIn

Folding Stats: Team CodeProject


GeneralRe: Images not held locally Pinmemberkiran dangar1-Jan-12 21:15 
GeneralRe: Images not held locally PinmemberSudeepPradhan15-Jan-12 20:37 
GeneralMy vote of 4 Pinmemberlavikgupta30-Dec-11 4:32 
Good Article.. But did not find the use of Model in Whole Execution Cycle
GeneralRe: My vote of 4 Pinmemberkiran dangar2-Feb-12 0:48 
GeneralMy vote of 4 PinmemberJyothikarthik_N28-Dec-11 18:30 
Reason for vote of 4 - Lucid and good.
GeneralRe: My vote of 4 Pinmemberkiran dangar2-Feb-12 0:46 
GeneralMy vote of 4 PinmemberArunkumar Subbaiyan19-Dec-11 20:11 
Why MVC? Where it will be useful?
GeneralMy vote of 4 Pinmembersaxenaabhi630-Nov-11 16:52 
nice one to quickly get your head around mvc.
GeneralRe: My vote of 4 Pinmemberkiran dangar30-Nov-11 18:34 
GeneralMy vote of 4 Pinmemberpurnananda behera15-Nov-11 21:02 
nice Description about MVC architecture.
GeneralRe: My vote of 4 Pinmemberkiran dangar30-Nov-11 18:38 
Questiongood PingroupJQuery Geeks21-Oct-11 17:44 
nice
AnswerRe: good Pinmemberkiran dangar21-Oct-11 17:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 14 May 2012
Article Copyright 2010 by kiran dangar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid