Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square brackets in the controller Route in ASP.NET Core

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
    {
        ...
    }
like image 557
qqqqqqq Avatar asked Dec 29 '19 22:12

qqqqqqq


People also ask

How to control routing in an ASP NET Core Application?

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).

What are controller attributes in ASP NET Core?

These attributes have metadata that tell ASP.NET Core when to call a specific controller. It is an alternative to convention-based routing.

What is matching route in ASP NET Core?

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.

What are route constraints in ASP NET Core?

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.


1 Answers

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.

like image 77
citronas Avatar answered Sep 19 '22 19:09

citronas