我正在尝试使用 IHttpContextAccessor 接口及其默认实现 HttpContextAccessor 从 DelegatingHandler 中读取发送到 WEB API 操作方法的 URL 参数。
控制器如下所示:
[HttpPost("{siteName}/{accountID}")]
public async Task<ActionResult<AirRequest>> Post(AirCModel model, string siteName, string accountID)
{
}
我想在 DelegatingHandler 中读取值 {siteName}/{accountID}
public class AuthenticationDelegatingHandler : DelegatingHandler
{
private readonly IHttpContextAccessor _httpContextAccessor;
public AuthenticationDelegatingHandler(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var siteNAme = _httpContextAccessor ???
}
}
在 Startup.cs 我注入了 HttpContext 服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddHttpContextAccessor();
}