What do the square brackets in the controller Route in ASP.NET Core mean?
E.g. the Route("[controller]") here:
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
...
}
There are a few ways of controlling the routing in an ASP.NET Core application, but in this article, we will concentrate on the two most common ways: Conventional routing: The route is determined based on conventions that are defined in route templates that, at runtime, will map requests to controllers and actions (methods).
These attributes have metadata that tell ASP.NET Core when to call a specific controller. It is an alternative to convention-based routing.
If there is a matching route entry, then it process the request i.e. serve the resource, otherwise it returns 404 error. There are two main ways to define routes in ASP.NET Core: It creates routes based on a series of conventions which represent all the possible routes in your system.
Route Constraints in ASP.NET Core. The Route Constraints helps us to filter out or restrict the unwanted values from reaching the controller action. It does so by checking the Constraint against the value of the URL Parameter.
It's a placeholder (called token replacement) for the controller's name without the suffix Controller
. In your case with WeatherForecastController
, [controller]
will automatically be replaced with WeatherForecast
.
You can find more details in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With