CODE

MVC in ASP.NET : An Introduction

wanted to go over a few of the basic principles of MVC in Asp.net since it’s a topic that can take a little bit of time getting your head around. I will also go into a little bit of extra detail regarding the Controller.

MVC (MODEL, VIEW, CONTROLLER)

A development methodology which uses the Microsoft Visual Studio development suite (also referred to as an IDE or Integrated Development Environment) through ASP.NET. This methodology is centred around breaking up an application’s development into 3 main parts: The Model, the View and the Controller.

Separations Of Concerns: Breaking up an application’s development into easy-to-handle, separate areas, is often referred to as a “Separation of Concerns” or SoC. By breaking it up into the Model, View and Controller,  it serves to simplify the process in a way where you can manage an application more effectively by focussing on one aspect of it at a time. This also makes it easier to test, debug and logistically manage the development of an app amongst several developers, as you break the various pieces of it up based on their purpose within the architecture.

MVC flowThis is the order with which these different parts interact and briefly describes what each piece is doing within the process:

VIEW (client) -> CONTROLLER (processes user request) -> MODEL (performs operations)

MODEL -> CONTROLLER (decides which VIEW to be rendered) -> VIEW (renders output)

THE MODEL (data logic) 

This handles the application’s data and logic and is the part of the application that is used to store and retrieve data from a database. Think of this as the place where all the back-end “business” operations are performed. Model classes hold data in public properties and this is where you get and set variables.

THE VIEW (user interface)

This is essentially the user interface, and therefore the part of the application that is immediately visible to the user. It receives information from the Controller and displays the information for users to view and interact with. This is where the application is rendered. This is a “.cshtml” file in the ASP.net framework.

THE CONTROLLER (request handler)

This basically handles user requests and returns a response.

The Controller and it’s action method handles incoming browser requests, retrieves necessary model data and returns appropriate responses.

 It will handle the user’s request, reading the data and user input received from the View, and sending this data to the Model to be processed. Think of this as where the connection between the Model and the View is made and those requests are processed. The controller will render the View, using the Model data. This is a “.cs” file in the ASP.net framework.

Note: The Controller class name must always end with the word “Controller”.

Action Methods: These are the PUBLIC methods() of a Controller class. They cannot be a STATIC method.

public ActionResult Index() //”ActionResult” is the return type, “Index” is the Action Method

{

return View(); //right click this and click Add View.. to create a view

}

Action Selectors: These are selectors which change the properties of the Action Method. They are usually shown in square brackets above the Action Method.

Action Name: This can be used to specify a different action name than the method name if you want a different word to be referenced in the URL pattern.

[ ActionName (“new_ActionMethod_name”) ]

public ActionResult old_ActionMethod_name (int id)

{

return View();

}

Non Action: This indicates that a public method of a Controller is not actually an Action Method. This is useful when you want to use a public method in a Controller but don’t want it to be treated as an Action Method.

[ NonAction ]

public Student GetStuentId(int id)

{

return something

}

Action Verbs: Can be used to define 2 different action methods that respond to 2 different Http request methods but have the same name (i.e. you can have one action method that “GETS” ((which is the default)), and one which “POSTS”.

//[ HttpGet ] is the default

public ActionResult Edit(int Id)

{

return View();

}

[ HttpPost ]

public ActionResult Edit(Student std)

{

return RedirectToAction(“Index”);

}

ROUTING

 In MVC, “routing” maps / creates a path between a URL pattern/request and a request handler (physical file or Controller Class and Action Method), instead of always mapping each URL to an actual file like ASP.net does with Web Forms. In Visual Studio, if you look in the App_Start folder in the Solution Explorer, you can see the RouteConfig.cs Class file. This is where the routes are registered/configured. Here, you can also create your own additional custom Route, but must keep the “Default” MapRoute in place. You can even add constraints to the routes:

routes.MapRoute(

name: “Default”,

url: “{controller}/{action}/{id}”,

defaults: new {controller = “Home”, action = “Index”, id = UrlParameter.Optional }

);

note: This URL pattern starts after the domain name. Also if you create a new Route, you will need to register it in the Application_Start event in the Global.asax.cs file.

C# / Asp.Net.