一、什么是 ASP.NET Core Attributes
ASP.NET Core Attributes 是一种标记(或注释)机制,用于在应用程序中添加元数据。它们可以用于控制应用程序的行为,例如路由、验证、过滤、缓存等。这些属性可以应用于控制器、操作方法、参数和模型。
二、常用的 ASP.NET Core Attributes
- Route Attribute
Route Attribute 用于定义控制器或操作方法的路由。它可以帮助开发者在应用程序中定义自己的 URL 格式。
使用方式:
Copy
[Route("api/[controller]")]
public class ProductsController : Controller
{
[HttpGet("{id}")]
public IActionResult GetProduct(int id)
{
//...
}
}
在上面的示例中,控制器被标记为 [Route("api/[controller]")],这意味着它的路由前缀将是 api/,后面跟着控制器的名称。操作方法被标记为 [HttpGet("{id}")],这意味着它将响应 GET 请求,并接受一个名为 id 的参数。
- HttpGet、HttpPost、HttpPut、HttpDelete Attributes
HttpGet、HttpPost、HttpPut、HttpDelete Attributes 用于定义操作方法的 HTTP 动词。它们通常与 Route Attribute 一起使用。
例如:
Copy
[Route("api/[controller]")]
public class ProductsController : Controller
{
[HttpGet("{id}")]
public IActionResult GetProduct(int id)
{
//...
}
[HttpPost]
public IActionResult CreateProduct([FromBody] Product product)
{
//...
}
[HttpPut("{id}")]
public IActionResult UpdateProduct(int id, [FromBody] Product product)
{
//...
}
[HttpDelete("{id}")]
public IActionResult DeleteProduct(int id)
{
//...
}
}
在上面的示例中,GetProduct() 方法被标记为 [HttpGet("{id}")],CreateProduct() 方法被标记为 [HttpPost],UpdateProduct() 方法被标记为 [HttpPut("{id}")],DeleteProduct() 方法被标记为 [HttpDelete("{id}")]。
- Authorize Attribute
Authorize Attribute 用于限制对控制器或操作方法的访问。它可以帮助开发者实现身份验证和授权。
使用方式:
Copy
[Authorize]
public class ProductsController : Controller
{
//...
}
在上面的示例中,ProductsController 类被标记为 [Authorize],这意味着只有经过身份验证的用户才能访问该控制器。
- AllowAnonymous Attribute
AllowAnonymous Attribute 用于允许未经身份验证的用户访问控制器或操作方法。
使用方式:
Copy
[AllowAnonymous]
public class ProductsController : Controller
{
//...
}
在上面的示例中,ProductsController 类被标记为 [AllowAnonymous],这意味着未经身份验证的用户也可以访问该控制器。
- ValidateAntiForgeryToken Attribute
ValidateAntiForgeryToken Attribute 用于防止跨站请求伪造(CSRF)攻击。它可以帮助开发者保护应用程序免受恶意攻击。
使用方式:
Copy
[ValidateAntiForgeryToken]
[HttpPost]
public IActionResult CreateProduct([FromBody] Product product)
{
//...
}
在上面的示例中,CreateProduct() 方法被标记为 [ValidateAntiForgeryToken],这意味着该方法必须在请求中包含有效的防伪标记。
- HttpHead、HttpOptions、HttpPatch Attributes
HttpHead、HttpOptions、HttpPatch Attributes 用于定义操作方法的 HTTP 动词。它们与 HttpGet、HttpPost、HttpPut、HttpDelete Attributes 相似,但通常用于更特殊的场景。
例如:
Copy
[HttpHead("{id}")]
public IActionResult GetProductHead(int id)
{
//...
}
[HttpOptions("{id}")]
public IActionResult GetProductOptions(int id)
{
//...
}
[HttpPatch("{id}")]
public IActionResult UpdateProductPartially(int id, [FromBody] JsonPatchDocument<Product> patchDoc)
{
//...
}
在上面的示例中,GetProductHead() 方法被标记为 [HttpHead("{id}")],GetProductOptions() 方法被标记为 [HttpOptions("{id}")],UpdateProductPartially() 方法被标记为 [HttpPatch("{id}")]。
三、如何在应用程序中使用 ASP.NET Core Attributes
在 ASP.NET Core 应用程序中使用 ASP.NET Core Attributes 可以帮助开发者实现更高效、更灵活的控制。ASP.NET Core Attributes 可以应用于控制器、操作方法、参数和模型。
例如:
Copy
[Route("api/[controller]")]
public class ProductsController : Controller
{
private readonly IProductRepository _productRepository;
public ProductsController(IProductRepository productRepository)
{
_productRepository = productRepository;
}
[HttpGet("{id}")]
public IActionResult GetProduct(int id)
{
var product = _productRepository.GetProduct(id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
[HttpPost]
public IActionResult CreateProduct([FromBody] Product product)
{
if (product == null)
{
return BadRequest();
}
_productRepository.AddProduct(product);
return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product);
}
[HttpPut("{id}")]
public IActionResult UpdateProduct(int id, [FromBody] Product product)
{
if (product == null || product.Id != id)
{
return BadRequest();
}
var existingProduct = _productRepository.GetProduct(id);
if (existingProduct == null)
{
return NotFound();
}
_productRepository.UpdateProduct(product);
return NoContent();
}
[HttpDelete("{id}")]
public IActionResult DeleteProduct(int id)
{
var existingProduct = _productRepository.GetProduct(id);
if (existingProduct == null)
{
return NotFound();
}
_productRepository.DeleteProduct(id);
return NoContent();
}
}
在上面的示例中,控制器被标记为 [Route("api/[controller]")],这意味着它的路由前缀将是 api/,后面跟着控制器的名称。操作方法被标记为 [HttpGet("{id}")]、[HttpPost]、[HttpPut("{id}")]、[HttpDelete("{id}")],这意味着它们将响应 GET、POST、PUT、DELETE 请求。参数被标记为 [FromBody],这意味着它们将从请求正文中读取数据。
四、总结
ASP.NET Core Attributes 是 ASP.NET Core 中的一种重要机制,它可以帮助开发者在应用程序中实现更高效、更灵活的控制。