1、先定义一个类 using Microsoft.AspNetCore.Http; namespace BCode.Util { public class MvcContext { public static IHttpContextAccessor httpContextAccessor; public static HttpContext GetContext() { HttpContext context = httpContextAccessor.HttpContext; return context; } } } 2、再启动类中指定 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHttpContextAccessor svp, IAntiforgery antiforgery) { MvcContext.httpContextAccessor = svp; } 来源: oschina 链接: https://my.oschina.net/u/4383170/blog/3384709