1.前置工作
1.注入
2.通用类的实现(代码简陋,各位有更好的可以艾特我),下面需要注意的是,header需要引入一个包Microsoft.AspNetCore.Http.Features和你net版本对应即可
public class HttpHeadersExtend { private IHttpContextAccessor accessor; // public string token { get; set; } public HttpHeadersExtend(IHttpContextAccessor _accessor) { accessor = _accessor; //token = HttpContext.Current.Request.Headers.GetValues("token");//Request } public string GetString(string key) { var dic = accessor.HttpContext.Request.Headers; if (dic.ContainsKey(key)) { return dic[key]; } return null; } }
3.把上面的类注入到环境中
2.需要的操作