.Net Core 获取上下文HttpContext

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

 

转载于:https://www.cnblogs.com/heibai-ma/p/11596143.html

你可能感兴趣的:(.Net Core 获取上下文HttpContext)