如何将当前 http 请求的用户绑定/注册到类



使用Web Api 2的Autofac,我可以将当前的http请求实例绑定到某个c#类。

如何在没有 autofac 的情况下使用 asp.net 核心执行此操作,只需使用

IServiceCollection services

您可以注册IHttpContextAccessor并将其注入到您的服务中

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

然后,您可以通过httpContextAccessor.HttpContext访问HttpContext。

最新更新