Delegating Handler trong APS.NET là gì?
Delegating Handler trong APS.NET là gì?
DelegatingHandler (Web API 2) là message handler nằm trong pipeline, xử lý request trước khi đến controller và response trước khi về client; có thể chặn/ chỉnh sửa/ ghi log (cross-cutting).
public class LoggingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
{
Log($"→ {request.Method} {request.RequestUri}");
if (!request.Headers.Contains("X-Api-Key")) return request.CreateResponse(HttpStatusCode.Unauthorized); // chặn sớm
var response = await base.SendAsync(request, ct); // chuyển tiếp
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.