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; }