Introduction to ASP.Net MVC

In this blog, we will know about ASP.NET MVC.
This is my first blog on MVC. 

We will see below topics in this blog –

1. What is ASP.NET MVC?
2. A relation between ASP.NET and ASP.NET MVC.
3. Why MVC?
4.The lifecycle of MVC application

1. What is ASP.NET MVC?

MVC – A commonly used software design pattern. MVC is a new architectural pattern introduced by Microsoft. MVC gives you cleaner and more maintainable code which makes developer life easy.

MVC consists of Model, View, and Controller.

Model – 

Model is the domain-specific classes in MVC architecture. It represents the data that users work with.

Sample example of Model in MVC –
The product is a Model class of an MVC application.

public class Products
        public int ProductId { get; set; }
        [Required ]
        public string ProductName { get; set; }
        public int ProductCost { get; set; }
        [Display (Name=“Discount Value”)]
        public int Discount { get; set; }

Domain Model –

Domain model is one of the most important part of MVC pattern. In ASP.Net MVC, domain model is the set of classes which define a specific business data.

For eg If you are creating a Human Resource System or Employee Payroll system, then Employee class will be your domain class or domain model which contains all the required information about an Employee like Name, Salary, Address, Designation.

View – 

A view in MVC is the user interface. It is similar to ASPX pages, but View in MVC may consist of Razor View Engine. We will see Razor View Engine in another blog. MVC views stored in Views folder. Action methods in Controller class can have their own Views to display the outcome.

You may add View for an Action in Controller by right-clicking in the Action method as shown below.

Either you may go to View or Add View if this action is not associated with any View.
Once you click on Add View, it will create a blank View for us.

Controller – 

A controller in MVC is responsible for handling the incoming requests. Controller class derived from System.Web.Mvc.Controller class.

Controllers receive the request and call the model, create the object of the model, call view and pass the data to the view to display the result.

Controller Class contains Action methods which return data to View after all the logic and manipulation based on a project. Controller classes found in Controller folder.

To add a Controller class, right click on Controller folder and select Add new controller.
You will get a box like this.

Remember not to remove Controller from Controller name. A Controller name must have suffix – Controller

You can name it DefaultController, Default1Controller, ProductController, MyProjectController etc

Once you add the Controller Class to your project. It looks like this –

public class Default1Controller : Controller
        // GET: /Default1/
        public ActionResult Index()
            return View();

Apart from Model, View, and Controller, there is one another important element of MVC is Routing.
To know more about Routing visit this blog – MVC Routing in Details

2. So what is the relation between ASP.Net and MVC?

ASP.Net 1.0 was released in year 2002, that time ASP.Net web forms are the way to develop web application in using .Net framework. ASP.Net is a framework for developing web applications and MVC (Model View Controller) is an architecture to arrange code in the better way.

3. Now a question may arise, why MVC?

MVC is not the only software design pattern, there are numerous other pattern. Then Why to choose MVC? Here we will talk in context of ASP.Net only.
As you know we were already doing development with traditional ASP.Net web forms, then what was the problem? Why are we looking for MVC architecture?

In any web application, the major factor is “Response Time”. How fast your page loads it decide the web page performance.

In ASP.Net we are using all server-side control. For eg –

<asp:TextBox ID=”txtBox1” runat=”server”/>

This control will render in as HTML because it is control which will cause an increase in load time which directly affect page performance.

In ASP.Net, everything is ASPx and .cs file where three-layer architecture can be implemented.

Now let’s move to benefit of MVC.
MVC has 3 sections Model, View, and Controller.

Here we can divide MVC in three-layer architecture as below.

A controller is equal to Code behind logic.

View is equivalent to ASPX/HTML page.

Model is middle layer.

In MVC, the first hit comes to the controller. Depending on the action, Controller creates the object of the model.

4. The lifecycle of an MVC Application

The lifecycle in MVC application always starts with Routing. All Routing must be associated with a Controller and Action method.

In ASP.Net aspx pages implements IHTTPHandler interface, this interface has a method called ProcessRequest().

But in MVC there are no physical pages. All requests mapped to a Controller class with their Action method.

So if a controller class is DefaultController and Action method is Index

then request URL pattern will be  http://localhost/Default/Index

A single controller class can handle multiple requests.

A basic MVC application lifecycle diagram below.

ASP.NET MVC application life cycle

Below is the sample code of Routing, Controller, Action, and View.

A Sample RouteConfig.cs

namespace MVCPractice
    public class RouteConfig
        public static void RegisterRoutes(RouteCollection routes)
                name: “Default”,
                url: “{controller}/{action}/{id}”,
                defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }

A Sample Controller Class with Action method Save()

namespace MVCPractice.Controllers
    public class ProductController : Controller
        public ActionResult Save()
            return View();

A sample View

    ViewBag.Title = “Save”;

Hope basics is clear to you.

I will start coding/development level blog after this. Till then enjoy learning MVC.

Next –

Please follow and like us:

Leave a Comment