Controllers in ASP.NET MVC 3


  • Controllers are the conductors of an MVC application.
  • They tightly controls the interactions of the user, the model objects, and the views.
  • The controllers are responsible for responding to user input, manipulating the appropriate model object, and then selecting the appropriate view to dislpay back to the user in response to the user input.
  • The ControllerBase class is the base class for all controllers, which provides general MVC handling.
  • The Controller class inherits from ControllerBase class and is default implementation of a controller.

The controller processes a user request with following stages:
1. Locating the appropriate action method to call.
2. The controller uses the user input as action method's arguments.
3. It handles all possible errors that migth occur during the execution of action method like model validation errors.
4. The controller provides the default WebFormViewEngine class for rendering ASP.NET page types (views)
5. Finally the view is rendered to the user.

The simple controller class looks like below:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Introduction.Controllers
{
    public class MVCIntroductionController : Controller
    {
        //
        // GET: /MVCIntroduction/

        public ActionResult Introduction()
        {
            return View();
        }

    }
}


All controller classes should end with controller suffix. The above controller class has Introduction action method which renders a view named Introduction.

When you create an empty project, controller directory is empty. In order to add a new controller right click on controller folder, and then click on Add and then click on Controller. On clicking controller a dialog box will open which asks for controller name you want to create.








Action Methods in Controller:
The job of action methods is to  respond to URL requests, perform the appropriate actions, and return a response back to the browser.
Action methods have one-to-one mapping with user interaction. The user interaction includes entering a URL into the browser, clicking a link and submitting the form. Each of these interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method.

When user enters URL, the MVC application uses the route rules specified in Global.asax file to parse the URL and determines the path of the controller.
The controller then decides the appropriate action method to handle the request. The URL entered by the user is considered as sub-path that includes controller and action method name.
                Suppose user entered http://20fingers2brains.com/Home/Logon, the sub-path in this URL is /Home/Logon where Home is the controller name and Logon is the action method name. The routing rule invokes the Logon action method of Home controller to process the request. 
     The URL can also be  http://20fingers2brains.com/Home/Logon/5 . In the above URL 5 is the value which will be sent to Logon action method as parameter.

0 comments:

Post a Comment