what is delegate handler in web api?
Ravi Patel
Select an image from your device to upload
Hello @ tomb of the mask, in ASP.NET Web API, a DelegatingHandler is a component that sits in the HTTP request/response pipeline and can inspect or modify requests before they reach the controller, and responses after the controller has processed them.
Think of it like middleware or a filter that wraps around the API request.
public class LoggingHandler : DelegatingHandler {protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken){// Before the request reaches the controllerConsole.WriteLine($"Request: {request.Method} {request.RequestUri}");var response = await base.SendAsync(request, cancellationToken);// After the controller has returnedConsole.WriteLine($"Response: {response.StatusCode}");return response;} }
The flow is roughly:
HTTP Request↓ DelegatingHandler↓ Authentication / Logging / Other handlers↓ Controller↓ DelegatingHandler↓ HTTP Response