In ASP.NET Core, square brackets are used to define optional route parameters in the controller Route attribute.
For example, consider the following code snippet:
[Route("api/[controller]")] public class ProductsController : ControllerBase { [HttpGet("{id?}")] public ActionResult<Product> Get(int? id) { //... } } In the above example, the [controller] token in the Route attribute will be replaced with the name of the controller, which is Products in this case. The HttpGet attribute defines a route for the Get action method with an optional id parameter. The ? character after the id parameter makes it optional. If the id parameter is not provided in the URL, the id parameter will be null.
So, when the URL https://localhost:5001/api/products is requested, the Get action method will be called with id parameter as null. When the URL https://localhost:5001/api/products/1 is requested, the Get action method will be called with id parameter as 1.
"ASP.NET Core controller Route attribute square brackets example"
[Route("api/[controller]")] public class MyController : ControllerBase { // Controller actions and methods } "ASP.NET Core route parameters in square brackets tutorial"
[Route("api/[controller]/{id}")] public IActionResult GetById(int id) { // Action logic to retrieve data by id } "ASP.NET Core attribute routing with square brackets best practices"
[Route("api/[controller]/[action]")] public class MyController : ControllerBase { // Controller actions and methods } "Customizing ASP.NET Core route templates with square brackets"
[Route("api/[controller]/[action]/{year:int}/{month:range(1,12)}")] public IActionResult GetByDate(int year, int month) { // Action logic for filtering data by date } "Nested square brackets in ASP.NET Core route attributes example"
[Route("api/[controller]/[action]/{category}/{id?}")] public IActionResult GetByCategory(string category, int? id) { // Action logic for filtering data by category and optional ID } "ASP.NET Core attribute routing constraints with square brackets"
[Route("api/[controller]/{id:int}")] public IActionResult GetById(int id) { // Action logic to retrieve data by integer id } "ASP.NET Core optional route parameters in square brackets example"
[Route("api/[controller]/{id?}")] public IActionResult GetById(int? id) { // Action logic to handle both cases: with and without id } "ASP.NET Core attribute routing with area and square brackets"
[Area("Admin")] [Route("admin/[controller]/[action]")] public class AdminController : ControllerBase { // Controller actions and methods for the admin area } "ASP.NET Core route tokens with square brackets overview"
[Route("api/[controller]/{action=Index}")] public class MyController : ControllerBase { // Default action logic } "ASP.NET Core global route prefixes with square brackets guide"
[Route("api/v{version:apiVersion}/[controller]")] [ApiController] public class MyController : ControllerBase { // Controller actions and methods } graph-tool file-uri cllocationmanager wildfly boggle ip-address lyx 64-bit windows-1252 data.table