ASP.NET MVC Introduction

Introduction to ASP.NET MVC Framework

In this article, I am going to give you a brief introduction to ASP.NET MVC Framework. As part of this article, we are going to discuss the following pointers in detail.

  1. What is ASP.NET MVC?
  2. What is MVC?
  3. How Does the MVC Design pattern Work in ASP.NET MVC Application?
  4. Advantages of using ASP.NET MVC to develop Web Application
What is ASP.NET MVC?

The ASP.NET MVC is a web application development framework provided by Microsoft which is built on top of the .NET Framework. We can use this ASP.NET MVC Framework to develop web applications which provide a clean separation of code. The ASP.NET MVC framework is the most extensible and customizable framework provided by Microsoft. 

The ASP.NET MVC Framework is based on MVC (Model-View-Controller) Design Pattern. So the point that I need to highlight is ASP.NET MVC is a Framework whereas MVC is a Design Pattern.

The ASP.NET MVC Framework is not built from ground zero. You can consider it is an alternative approach to our traditional ASP.NET Web Forms Framework. As it is built on the top of the .NET Framework, developers enjoy almost all the ASP.NET features while working with the MVC application.

What is MVC?

MVC is an architectural software design pattern which is used for developing interactive applications where there would be user interaction involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based application where there are user interactions involved.

MVC design pattern was introduced in the 1970s that basically divides an application into 3 major components such as Model, View, and Controller. The main objective of the MVC design pattern is the separation of concerns, it means the domain model and business logic are separated from the user interface (i.e. view). Thus, maintenance and testing of the application become simpler and easier.

How does the MVC Design Pattern work in ASP.NET MVC Application?

Let us understand how does the MVC Design Pattern work in ASP.NET MVC application with an example. Let say to display the student details in a web page as shown in the below image.

How does the MVC Design Pattern work in ASP.NET MVC Application

So, when the client (user) issues a request something like “http://dotnettutorials.net/student/details/2” from a web browser then the request handled by the MVC framework as shown below.

ASP.NET MVC Framework Model View Controller Diagram

The controller is the component who actually receives the incoming HTTP request and then handle that request. In order to handle the incoming HTTP request, the controller does several things are as follows.

The controller creates the model object if that is required by a view. The model is the component in MVC design which contains a set of classes to represent the domain data or business data as well as logic to manage the data. 

The controller then selects a view to render the domain data or business data. The point that you need to remember is, while selecting a view, it is the responsibility of the controller to pass the model data.

In the MVC, the one and only responsibility of a view to display the model data. So, the responsibility of view is to generate the necessary HTML in order to render the model data. Once the HTML is generated by the view, then that HTML is then sent to the client who initially made the request.

Let us discuss each of the components of the MVC design pattern in detail.

Model:

The Model is the component in MVC design pattern that manages that data i.e. state of the application in memory. The Model contains a set of classes that represent the data as well as logic to manage the data. So, in our example, the model is consists of Student class to represent the student data as well as StudentBusinessLayer class to retrieve the student data from any persistent medium like a database. 

Model in ASP.NET MVC Application

So in short, a Model:

  1. In ASP.NET MVC is basically a C# or VB.net class to represent the data as well as to manage the data.
  2. Is accessible by both controller and view.
  3. Can be used to pass data from controller action methods to a view.
  4. Can also be used by a view to display data in a page (HTML output).
View:

The view is the component in MVC Design Pattern which renders the model data as the user interface with which the end-user can interact. So, the View creates the user interface with data from the model. In our example, we want to display the Student information in a web page. So here the student model carried the student data to the view. This is the student model which should be supplied by the controller to the view. The following code does the same thing.

@model FirstMVCApplication.Models.Student
<html>
<head>
    <title>Student Details</title>
</head>
<body>
    <br />
    <br />
    <table>
        <tr>
            <td>Student ID: </td>
            <td>@Model.StudentID</td>
        </tr>
        <tr>
            <td>Name: </td>
            <td>@Model.Name</td>
        </tr>
        <tr>
            <td>Gender: </td>
            <td>@Model.Gender </td>
        </tr>
        <tr>
            <td>Branch: </td>
            <td>@Model.Branch</td>
        </tr>
        <tr>
            <td>Section: </td>
            <td>@Model.Section </td>
        </tr>
    </table>
</body>
</html>

So in short, a View

  1. In ASP.NET MVC is a cshtml page.
  2. Contains all page specific HTML generation and formatting code.
  3. A request to a view can only be made from a controller’s action method.
  4. The one and only responsibility of a view to render the domain data.
Controller:

It contains the control flow logic. It is the one which will interact with both models and views to control the flow of application execution. The controller is the component in MVC Design Pattern which will handle the incoming HTTP Request. Based on the user actions, the respective controller will work with the model and view and then sends the response back to the user who initially made the request. In our example, when the client issued a request to the following URL

http://dotnettutorials.net/student/details/2

Then that request is going to be mapped to the Details action method of the Student Controller. Following is the code of our Controller class with the Details action method.

Controllers in ASP.NET MVC

So, in short, a Controller:

  1. Is basically a C# or VB.NET class which is inherited from the System.Web.Mvc.Controller.
  2. Is the component which will interact with both Models and views.
  3. Contains action methods which are responsible for handling the incoming URL.
  4. Can access and use the model class to pass the data to the views.
Advantages of using ASP.NET MVC to develop Web Application
  1. It is lightweight because it does not use view state or server-based forms or server controls.
  2. Each developer based on his expertise or experience can work on different parts of the application. For example, one developer may work on the view while the second developer can work on the controller logic and the third developer may work on the business logic.
  3. Clean HTML and easy integration with javascript and jQuery.
  4. It provides better support for test-driven development (TDD). This is because we can focus on one aspect at a time i.e. we can focus on the view without worrying about business logic.
  5. ASP.NET MVC Framework divides the application into three main aspects such as Model, View, and Controller which make it easier to manage the application complexity.
  6. Another important advantage of ASP.NET MVC framework is its components are designed to be extensible and pluggable and therefore they are easily replaced or customized.
  7. The MVC framework is built on top of the ASP.NET Framework and hence we can use most of the ASP.NET features such as authentication and authorization scenarios, membership and roles, caching, session and many more.
  8. ASP.NET MVC framework supports a powerful URL routing mechanism (i.e. attribute routing) which helps to build a more user-friendly and SEO friendly URLs for our application.

In the next article, I am going to discuss the step by step process to create ASP.NET MVC application using Visual Studio. Here, in this article, I try to give you a brief introduction to ASP.NET MVC Framework. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *