.Net Core 獲取上下文HttpContext

一、先定義一個類app

using Microsoft.AspNetCore.Http;
namespace BCode.Util
{
    public class MvcContext
    {
        public static IHttpContextAccessor httpContextAccessor;
        public static HttpContext GetContext()
        {
            HttpContext context = httpContextAccessor.HttpContext;

            return context;
        }

    }
}

 

二、再啓動類中指定ui

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHttpContextAccessor svp, IAntiforgery antiforgery)
        {
            
            MvcContext.httpContextAccessor = svp;
    }
相關文章
相關標籤/搜索