attribute
A tag in square brackets, such as [Required] or [HttpGet], that attaches extra information to code for the compiler or framework to read.
In more detail
Attributes do nothing on their own; some other code has to look for them, often through reflection. ASP.NET Core reads attributes to route controller actions, bind parameters and validate models.
Related terms
- data annotations — Attributes from System.ComponentModel.DataAnnotations, such as [Required] and [Range(1, 200)], that declare simple validation rules on properties.
- reflection — Code inspecting types, properties and methods at run time instead of knowing them at compile time.
- attribute routing — Declaring a controller's routes with attributes such as [Route("api/products")] and [HttpGet("{id:int}")] on the class and its actions.
Taught in
- The Options Pattern: Strongly Typed Configuration — Modern .NET Core, C# & Enterprise Microservices
- Building Routes with Controllers — Modern .NET Core, C# & Enterprise Microservices
- Source-Generated Mapping with Mapperly — Modern .NET Core, C# & Enterprise Microservices