Actions in ASP.NET Core MVC

Actions are methods within your controller classes that handle incoming HTTP requests. When a request matches a route, ASP.NET Core MVC determines which action method in which controller to execute.

Defining Action Methods

Action methods are public methods within a controller class. They can return various types, most commonly IActionResult or its derived types like ViewResult, JsonResult, ContentResult, etc. By convention, action methods are named descriptively to indicate their purpose.

Example: A Simple Action

Consider a controller named HomeController:

using Microsoft.AspNetCore.Mvc;

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View(); // Returns a view named "Index"
    }

    public string Hello()
    {
        return "Hello from the Hello action!"; // Returns plain text
    }

    public IActionResult About()
    {
        ViewBag.Message = "Your application description page.";
        return View(); // Returns a view named "About"
    }
}

Action Method Return Types

The return type of an action method determines how the response is generated. Common return types include:

Action Selection

ASP.NET Core MVC uses a sophisticated mechanism to select the appropriate action method based on the incoming request URL and the defined routes.

Action Method Overloading

You can overload action methods, but they must have different signatures. For example, you can have two methods named Details, one accepting an integer ID and another accepting a string slug.

public IActionResult Details(int id) { /* ... */ }
public IActionResult Details(string slug) { /* ... */ }

However, if you have multiple overloads that could potentially match a request based on routing, you might need to use action method selectors to disambiguate.

Action Method Selectors

Action method selectors allow you to specify criteria for matching a request to a particular action method. This is particularly useful when you have multiple action methods with the same name or when you want to constrain actions to specific HTTP methods.

Common Selectors:

Example with Selectors:

using Microsoft.AspNetCore.Mvc;

public class ProductsController : Controller
{
    [HttpGet]
    public IActionResult List()
    {
        // Logic to retrieve a list of products
        return View();
    }

    [HttpGet("{id:int}")] // Route constraint for an integer ID
    public IActionResult Details(int id)
    {
        // Logic to retrieve product details by ID
        return View();
    }

    [HttpPost]
    public IActionResult Create(Product product)
    {
        if (ModelState.IsValid)
        {
            // Logic to save the product
            return RedirectToAction("List");
        }
        return View(product); // Redisplay form with validation errors
    }

    [ActionName("OldDetails")]
    [HttpGet("products/details/{slug}")]
    public IActionResult GetProductBySlug(string slug)
    {
        // Logic to retrieve product by slug
        return View("Details"); // Reuse the Details view
    }
}

Important Considerations

When returning a view, the framework looks for a view file that matches the action method name (e.g., Index.cshtml for the Index action) in common view locations. You can also explicitly specify the view name or path.