I’m fairly new to the .net Framework and the whole MVC programming philosophy. Could someone clarify and give me a basic explanation how controllers interact with sites using C#? I understand how to code in C#, and I understanding some aspects of the framework, but I don’t see how they all tie together.
I’m fairly new to the .net Framework and the whole MVC programming philosophy. Could
Share
Model – Is a data structure that represents some kind of object (usually one). It’s purpose is to read, write and manage the access to the underlying object with the aim to persist application state.
View – Is the components that are used to display a visual interface to the user, perhaps using a model. It might be a simple table or a complex combination into a full web page.
Controller – Is the user driven application logic layer the sits between views and models. It handlers user interaction, loads models, and sends views to the user. It determines what model is sent to the view depending on user requests.
The overall folder structure for an application might look like this.
In C# MVC each controller must have the suffix
Controllerin the name, they must extend Controller class and have a folder of the name prefix (without theController) in the views folder. This folder will then contain all the views related to particular actions on the controller.Controllers can contain any number of actions defined as public functions. By default when returning a result from a controller action the name of the view must correspond with the name of the action. However you can also specify a view by name. When loading a view from a controller, it is possible to send an object as a model to the view and there by generate it’s content.
Controllers can load any model and are not restricted in any way.
An
Accountcontroller defined as below with an actionLogin. The controller is placed in aAccountController.csfile in the/Controllersfolder, and any views for this controller (Loginin this instance with filenameLogin.cshtml) are placed in the/Views/Accountfolder.Note: The naming convention has to be right as the names are used between the controllers and views to link the data.
would be accessible via
http://www.mysite.com/Account/Login. If the user is authenticated, the controller will redirect to the main site controller, if the user is not logged in then they are shown theLoginview which loads data from theLogOnModelspecified.This is really just touching the surface of what is possible. Read some online information on some excellent articles by ScottGu which go into much more depth and talk you through how to use MVC.
ASP.NET MVC Framework Overview
ASP.NET MVC Framework How To – Part 1
// Part 2
// Part 3
// Part 4
Note : These articles are slightly outdated as they were written for MVC version 1 back in 2007, but the concepts of how the Models, Views and Controller interact still apply.