What are controller actions?

Asked

Viewed 858 times

6

In that reply the Gypsy says

This (example) is good when you have several Actions in the Controller

What are the Actions of a Controller?

Note: I would like some example of code as well, defining a controller with two or more actions and see how those actions are "called".

1 answer

11


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

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

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

In order for a class to be considered a controller, it must follow some rules.

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

A controller can contain several actions (Actions). Actions are used to process requests made by browsers. To create an action, it is necessary to define a public method within the class of a controller. The parameters of this method can be used to receive the data sent by users through HTML forms. This method must return a Actionresult which will be used by ASP.NET MVC to define what should be executed after the action ends.

By default, when we create an ASP.NET MVC project in Visual Studio, a route with the format {controller}/{action}/{id} is added to the route table. With this route, if a request is made to the url http://www.k19.com.br/Editora/Listagem, the controller defined by the class Editoracontroller and the action implemented by the List() 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.