abp vnext请求头增加,以及请求头增加公共头部回调
context.Services.AddHttpContextAccessor();
要访问请求头的话,要加上这个语句;
- 追加请求头的方法:
1、服务层服务的HttpApi.Client项目Module类的ConfigureServices方法开头位置添加如下代码:
context.Services.AddTransient<AddHeaderHandler>();
context.Services.AddHttpClient(ProductManagerRemoteServiceConsts.RemoteServiceName)
.AddHttpMessageHandler<AddHeaderHandler>();
在一个JGSoftAbpClientModule添加AddHeaderHandler类如下:
public class AddHeaderHandler : DelegatingHandler, ITransientDependency
{
private ICurrentUserExtension_currentUserExtension;
public AddHeaderHandler(ICurrentUserExtension currentUserExtension)
{
_currentUserExtension = currentUser;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var headers =request.Headers;
if (!headers.Contains("Systemno"))
{
headers.Add("Systemno",_currentUserExtension.Systemno);
}
return await base.SendAsync(request, cancellationToken);
}
}