What are the actions of a controller?

6

In this answer Gypsy says

  

This (example) is good here when you have multiple Actions on the Controller

What are the Actions of a Controller ?

Note: I would like some code example too, defining a controller with two or more actions and see how those actions calls. "

    
asked by anonymous 21.10.2015 / 18:35

1 answer

11

In ASP.NET MVC urls are mapped to methods in the classes that define the so-called controllers (Controllers). The requests sent by browsers are processed by the controllers.

The processing performed by a controller to handle a request basically consists of:

  • Retrieve data sent by the user through forms
  • Interact with the template layer.
  • Activate the presentation layer to build the HTML page that should be sent to the user in response to your request.

For a class to be considered a controller, it must follow some rules.

  • The class name must have the suffix "Controller".
  • The class must implement the interface System.Web.Mvc.IController or inherit from class System.Web.Mvc.Controller

A controller can contain several actions. Actions are used to process requests made by browsers. To create an action, you must define a public method within a controller class. The parameters of this method can be used to receive data sent by users through HTML forms. This method should return an ActionResult that will be used by ASP.NET MVC to define what should be done after the action is finished.

By default, when you create an ASP.NET MVC project in Visual Studio, a {controller} / {action} / {id} route is added to the routing table. With this route, if a request is made to the link url, the controller defined by the class EditoraController and the action implemented by the Listing () method of this class will be chosen to process this request.

Example:

public class EditoraController
{
  public ActionResult Listagem()
  {
    return View();
  }
}

Source: K32 - Web Development with ASP.NET MVC Page: 129

    
21.10.2015 / 18:43