Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding MVC4 Controllers

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.

like image 763
Taylor Jones Avatar asked Jan 22 '13 16:01

Taylor Jones


People also ask

What is controller in MVC?

Understanding Controllers.. Asp.net MVC Controllers are responsible for controlling the flow of the application execution. When you make a request (means request a page) to MVC applications , a controller is responsible for returning the response to that request. A controller can have one or more actions.

What is the role of the studentcontroller in MVC?

Controller and its action method handles incoming browser requests, retrieves necessary model data and returns appropriate responses. In ASP.NET MVC, every controller class name must end with a word "Controller". For example, the home page controller name must be HomeController, and for the student page, it must be the StudentController.

What is Model-View-Controller in ASP NET MVC?

Moreover, controllers in Asp.Net MVC, respond to HTTP requests and determine the action to take based upon the content of the incoming request. By default, controllers are stored in the Controllers folder of the project. What do you think? I hope you have got what is Model-View-Controller in Asp.Net MVC.

What are index () and about () methods of homecontroller in MVC?

The sample ASP.NET MVC application includes a controller named HomeController.cs located in the Controllers folder. The content of the HomeController.cs file is reproduced in Listing 2. Notice that the HomeController has two methods named Index () and About (). These two methods correspond to the two actions exposed by the controller.


1 Answers

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.

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

In C# MVC each controller must have the suffix Controller in the name, they must extend Controller class and have a folder of the name prefix (without the Controller) 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 Account controller defined as below with an action Login. The controller is placed in a AccountController.cs file in the /Controllers folder, and any views for this controller (Login in this instance with filename Login.cshtml) are placed in the /Views/Account folder.

Note: The naming convention has to be right as the names are used between the controllers and views to link the data.

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

        return View("Login", new LogOnModel());
    }
}

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 the Login view which loads data from the LogOnModel specified.

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.

like image 121
Kami Avatar answered Oct 18 '22 10:10

Kami