TOC

The community is working on translating this tutorial into Chinese, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Controllers:

Actions

Since a Controller is just a regular .NET class, it can have fields, properties and methods. Especially the methods of a Controller class are interesting, because they are the connection between the browser (and therefore the user) and your application. For that reason, the methods of a Controller class is referred to as actions - a method usually corresponds to an action in your application, which then returns something to the browser/user.

Since a browser works by calling a URL, you need something that translates URL's to a corresponding Controller and action (method). For instance, the browser might request a URL like /products/1/ and then you want your ProductsController to handle this request with a method/action called Details. This is done with the concept of Routing, something we'll talk much more about elsewhere in this tutorial, but for now, just know that Routing is what connects URL's to actions on your Controllers.

When creating your Controllers, keep in mind that all public methods on a Controller class is considered an Action. This means that if you have defined catch-all routing rules for your Controller (and that is a common thing to do), all the methods on your Controller class can in theory be reached using a URL. So if you have methods on your Controller that you don't want the end-user to be able to call, be sure to mark it as private. As an alternative, if you really need a method to be public but not accessible by URL, you can mark the method with the [NonAction] attribute.

Action Verbs

To gain more control of how your actions are called, you can decorate them with the so-called Action Verbs. These are in fact regular .NET attributes, which will tell the .NET framework how an action can be accessed. Without these attributes, an action can be accessed using all possible HTTP methods (the most common ones are GET and POST), but you can change that pretty easily:

[HttpGet]
public IActionResult Edit()
{
return Content("Edit");
}

Now the Edit action can only be accessed with a GET request. This has the added benefit of allowing you to have multiple methods with the same name, as long as they don't accept the same request method. So for instance, you could have two methods called Edit: The first one would be available for GET requests and generate the FORM for editing an item, while the second one would only be available for POST requests and be used to update the item when the FORM was posted back to the server. It could look like this:

[HttpGet]
public IActionResult Edit()
{
return View();
}

[HttpPost]
public IActionResult Edit(Product product)
{
product.Save();
return Content("Product updated!");
}

Now whenever a request is made to the Edit() method/action, the actual method responding to the request will be based on whether it's a GET or a POST request.

In some situations, you may want to specify multiple Action Verbs, e.g. to specify that an action can be accessed by both POST and GET requests but not other types. That's just as easy:

[HttpGet]
[HttpPost]      
public IActionResult Edit()
{
...

Summary

We have now learned more about one of the most important aspects of a Controller: The Actions, sometimes referred to as Action Methods or just methods, since that's what they basically are. In the next article we'll discuss the result of these actions, referred to as Action Results.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!