VIEW ARTICLE

Implementing MVC Architecture in ASP.NET

Author: Surinder P Kumar
Category: Microsoft
Section: Tech.Architecture
Related Questions: 0 Login to Bookmark
Submitted On: 5/10/2017
Published On: 6/12/2017
ABSTRACT: This article discusses various approaches for implementing MVC architecture in web based application.

ASP.NET technology, is a part of the .NET framework and enables the developer to create dynamic responses (on the server) to user requests landing on the server via HTTP. The client only needs a browser (that supports JavaScript) on the machine to generate requests. The server runs the code in the .NET framework and responds to user requests.

The MVC architecture is built-in when we choose ASP.NET MVC template from Visual studio. We just have to maintain it by ensuring that our coding, methods etc do not voilate the pattern. For example, the View lets the developer to write C#/server-code code in itself. But the developer has to ensure that he/she writes minimum amount / only required code in the View file. Most of the code is desired to be written in controller only. When written in controller, the code converts into the relevant dll file for deployment.

ASP.NET implements MVC which is based on a specific software architecture pattern, called Model-View-Controller. This pattern defines a model that implements data entities and data access, a View that represents information to show to the user, and a Controller that controls the Model, uses it and sends data to the View.



The controller receives the request from the user browser and sends the response back to the user. In the process of building response for the user, the controller holds the processing controll. It manipulates things, may call in models as per their definitions, to provide required data from data sources, uses View to formulate the required response. When ready, the View is served to the client browser as a response.

Separation of concerns
In this software pattern, the Controller and Model are generally written in a server side language like C# in .NET development framework, and are run on the server. The View is typically for the user interface, and is written mostly in HTML code with JavaScript language. It contains least of C# code just to access server-side infromation.

Due to separation of concerns, the biggest advantage that we get is that unit tests can easily test the functionality of various methods, and modules put together. We know that controllers control the most part and contains methods with parameter /arguments and return values, and these can be covered with unit tests easily.

The Controller is a class in the system that derives from the "Controller" class.

        
namespace MyProgram.Controllers
{
    public class MyTaskController : Controller
    {
        public ActionResult Index()
        {
            if(loggedIn == false)
                return RedirectToAction("Index", new { id = sdf });
            else
                return View();
        }
    }
}
        
        

The Model is a class in the system that does not necessarily to be derived from any class.

        
amespace MyProgram.Models

        using System;
        using System.Collections.Generic;

        public partial class City
        {
    
            public City()
            {
                this.Users = new HashSet();
            }

            public int CityID { get; set; }
            public string City { get; set; }
            public int CountryID { get; set; }
            public int StateID { get; set; }

            public virtual Country Country { get; set; }
            public virtual State State { get; set; }
            public virtual ICollection Users { get; set; }
        }
}
        
        

The View is an html file with .chtml extension.



In a typical program, there are generally a number of Controllers written for various activities/functions and in return, these controllers use their relevant Views, partial-views, and models, View-models, to produce a meaningful functionality.


Go to Top
Implementing MVC Architecture in ASP.NET

Related Questions...(0) Ask A Question

Ask a Question

Go to Top