A controller groups C# methods called actions. An action handles an HTTP request and produces a response. Controllers are one way to build both JSON web services and HTML applications in ASP.NET Core MVC; grouping related actions in one class keeps their request handling together. section 2.3.1 section 19.4
Start with the class, then decide how requests reach its actions:
- Name a public class with a Controller suffix. MVC discovers it at startup even if it is outside the conventional Controllers folder. Public methods are treated as actions. Deriving from ControllerBase or Controller provides MVC features, but inheritance is not required for discovery. section 19.4.2
- For a web service, use Route on the class and HTTP-method attributes on actions. For example, api/[controller] on ProductsController and HttpGet("{id}") select GET api/products/{id}. section 19.4.2
- For an HTML application, derive from Controller to get the View method. Convention-based routing can map the default URL to HomeController.Index without Route or HTTP-method attributes. section 21.2.2
Listing 19.6 The contents of the ProductsController.cs file in the Controllers folder
using Microsoft.AspNetCore.Mvc;
using WebApp.Models;
namespace WebApp.Controllers {
[Route("api/[controller]")]
public class ProductsController : ControllerBase {
[HttpGet]
public IEnumerable<Product> GetProducts() {
return new Product[] {
new Product() { Name = "Product #1" },
new Product() { Name = "Product #2" },
};
}
[HttpGet("{id}")]
public Product GetProduct() {
return new Product() {
ProductId = 1, Name = "Test Product"
};
}
}
}
The ProductsController listing shows two GET actions in one class: one for the product collection and one for a URL containing an ID. section 19.4.2
A controller can receive a data service through its constructor; an action can receive a service through FromServices. Model binding can supply an action parameter from the request URL or a Product object from a JSON request body. A new controller instance is created when an action handles a request, so its request-related properties describe that request. section 19.4.2
Listing 21.5 The contents of the HomeController.cs file in the Controllers folder
using Microsoft.AspNetCore.Mvc;
using WebApp.Models;
namespace WebApp.Controllers {
public class HomeController : Controller {
private DataContext context;
public HomeController(DataContext ctx) {
context = ctx;
}
public async Task<IActionResult> Index(long id = 1) {
return View(await context.Products.FindAsync(id));
}
}
}
The HTML example injects a data context, retrieves a product in Index, and passes it to View. The returned ViewResult tells MVC to render a view using that product as its model. section 21.2.2
Add an RSVP page
Put an RsvpForm action on HomeController and return View(). Create Views/Home/RsvpForm.cshtml, then request http://localhost:5000/home/rsvpform. With no view name supplied, the action name selects RsvpForm.cshtml; the view generates the HTML response. If the view is missing, MVC reports an error rather than inventing a page. section 3.2.3 section 21.2.2







