MVC (Model View Controller) Interaction with Browser
Inside MVC Architecture
What is a Model?
- MVC model is basically a C# or VB.NET class
- A model is accessible by both controller and view
- A model can be used to pass data from Controller to view
- A view can use model to display data in page.
What is a View?
- View is an ASPX page without having a code behind file
- All page specific HTML generation and formatting can be done inside view
- One can use Inline code (server tags ) to develop dynamic pages
- A request to view (ASPX page) can be made only from a controller’s action method
- Controller is basically a C# or VB.NET class which inherits system.mvc.controller
- Controller is a heart of the entire MVC architecture
- Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views.
- Controller can access and use model class to pass data to views
- 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.
- Root [directory]
- Controller [directory]
- Controller CS files
- Models [directory]
- Model CS files
- Views [directory]
- View CS files
ASP.NET MVC Execution Life Cycle
Browser Request (Step 1)
Job of Global.asax – MVC routing (Step 2)
application_start()method inside Global.asax file. From the requested URL, it will parse the Controller, Action and ID.
- 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)
index()in our case.
homeController.index()will happen inside MVC controller class.
Call to View (Step 4)
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.
index(). So calling
view()will return a rendered HTML from the ASPX page located at/views/home/index.aspx.