The beauty of MVC (Model – View – Controller) design lies in the distribution of responsibilities it provides due to its architecture. This has been the sole reason why MVC has dominated as the sole design pattern for simplifying monolith applications having extensive UI and business logic.
MVC is generally used to refactor huge UI classes that hold all the UI events along with the related application logic. The latest web application framework to adopt this pattern is ASP.Net. The result of this is ASP.Net MVC.
However, the use of MVC brings a complete change in the concept of web applications. Let us understand what is this change:
Request – Response flow
- A general ASP.Net application is a collection of files(generally aspx files) hosted on the web server at a specific location ( path).
- A client which wants to access any of these files requests for the file by specifying the location.
Eg : A file help.aspx which is kept in folder /Samples on the server is requested as follows – http://server:port/Samples/help.aspx which raises the following request GET /Samples/help.aspx
- The server responds by returning the contents of the aspx files in HTML format, which is then rendered by the client mostly a browser.
Hence, an ASP.Net application is nothing but a collection of aspx files which are hosted on the server. Whenever a request is made one of these files are send back to the client (This is nothing but the flow of HTTP GET command) .
However, the introduction of MVC changed this complete scenario. An ASP.Net application is no more a collection of aspx files. It is now a service which holds executable methods (also called as actions ). The client instead of requesting for an aspx file, now needs to request the execution of one of these actions. The result of this execution is then returned back to the client. Let us see how this works.
MVC (Model – View – Controller)
Let us first begin by understanding – what is MVC and how it is implemented in ASP.Net MVC.
The main idea behind an MVC pattern is to seperate the control logic from the user interface. The MVC pattern divides the ASP.Net application into three parts – Model, View and Controller.
- The Controller acts as the brain of the application and holds most of the application logic.
- The View handles\ manages the display of information. In other words, it represents the user interface (web pages) of the application. However the views are dumb i.e they do not hold any domain logic (application logic) and are decoupled from any data access calls (i.e no data access calls or other kind of calls can be made from the view). Hence, the UI can stay the same, even if there is a change in the domain or data access logic, within the application.
- The Model represents the data source. This could be databases, text files, XML files etc. Every element related to fetching, storing or manipulation of information comes under Model.
- The controller is the most important component, which is also incharge that decides what model objects to use and what views to render.
- The controller is in charge of coordination and is the first to get executed, when a web request comes to the application.
- The controller is responsible for deciding what response is appropriate for the request
Let us now understand how the MVC has changed the face of ASP.Net web application.
- As said earlier, the ASP.Net application is no more a collection of aspx files, but it is now a service which carries out activities on requests.
- An ASP.Net MVC application is internally, a collection of MVC triads.
- When a request comes in, it is handled by a routing engine. The routing engine decides what controler and action should it use to serve that request. The actions are simply public methods on a controller.
- Once the actions are executed, steps relevant to that actions are carried out and the control is handed over to the Views, where the data is rendered to HTML and returned to the client.
- Hence every action is performed by a controller. The final result of this action is generally a View which is returned back to the client. Hence Views are also called as ActionResult (i.e result of an action) in ASP.Net MVC.
Hence, in case of ASP.NET MVC, a client is no more requesting a resource from the server, but its requesting the execution of an action (method) on the server, the result of whcih is returned back to the client in the form of HTML.
So, ASP.NET application (using ASP.NET MVC framework) works with methods and not files.