Understanding MVC4 Controllers

I am new to the .NET Framework and the entire MVC programming philosophy. Can someone clarify and give me a basic explanation of how controllers interact with sites using C #? I understand how to write code in C #, and I understand some aspects of the structure, but I don’t see how they are all related to each other.

+4
source share
4 answers

Model . It is a data structure representing an object (usually one). It is designed to read, write and control access to the base object in order to maintain the state of the application.

View . Are the components that are used to display the visual interface to the user, possibly using a model. It can be a simple table or a complex combination on a full web page.

Controller Whether the user logic level of the application sits between views and models. It handles user interaction, loads models, and sends views to the user. It determines which model is sent for viewing depending on user requests.

The general folder structure for an application might look like this.

>> Website >> Controllers >> Models >> Views 

In C # MVC, each controller must have a Controller suffix in the name, it must extend the controller class and have a folder with a name prefix (without Controller ) in the views folder. This folder will contain all views related to specific actions on the controller.

Controllers can contain any number of actions defined as public functions. By default, when a result is returned from a controller action, the name of the view must match the name of the action. However, you can also specify the view by name. When loading a view from the controller, you can send the object as a model to the view and create its contents.

Controllers can load any model and are in no way limited.

The Account controller defined below with the Login action. The controller is placed in the AccountController.cs file in the /Controllers folder, and all views of this controller ( Login in this case with the name of the Login.cshtml file) are placed in the /Views/Account folder.

Note. The naming convention must be correct, as names are used between controllers and views to bind data.

 public class AccountController : Controller { public ActionResult Login(string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index","Site"); } return View("Login", new LogOnModel()); } } 

will be available through http://www.mysite.com/Account/Login . If the user is authenticated, the controller will be redirected to the main site controller, if the user does not log in, then the Login view will be displayed, which loads the data from the specified LogOnModel .

This really only applies to what is possible. Read some online information about some of the great ScottGu articles that go much deeper and tell you 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 a bit dated because they were written for MVC version 1 in 2007, but the concepts of interaction between models, views, and the controller still apply.

+5
source

Controllers serve as an internal web service. They display your server side code for your views and allow them to call controllers. Regarding the model, most people think that controllers should be as thin as possible. If there is heavy lifting or other business logic, you should relate it to another part of your application. In my opinion, controllers should provide a view with something to call, and then return this data, be it text / html, json or xml.

There is a lot of information right from the source: http://www.asp.net/mvc/mvc4

In particular, on the site I highly recommend the tutorial. This will give you a clearer picture of how Models, Views, and Controllers interact and depend on each other. http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

+3
source

A controller is a class that has methods, these methods are called actions, you can use these actions for "views" (cshtml files).

 //This is your controller public class HomeController : Controller { // This is your action public ActionResult Index() { return View(); } } 

You can right-click on the "Index" action and select "Add View ...", this will create a new view attached to this action.

To access this view, you will do something like: localhost / Controller / Action In this case it should be: localhost / Home / Index, where Home = Controller, Index = Action

You should read the MVC pattern

Microsoft has really good beginner tutorials

+1
source

A controller in ASP.NET MVC is an object that processes your application logic in response to requests. It will be created for each request (for example, an HTTP request) and will be available until the response created by the View layer. After that, it will become unusable for the object (and soon GC will free the allocated memory), and for another request a new controller object must be created, etc.

I think that by this definition it will be obvious why it should be easy and how you should use it.

+1
source

All Articles