netcore中使用HttpContext.Current

新建HttpContext类

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Web.Common
{
    /// 
    /// http上下文
    /// 
    public class HttpContext
    {
        private static IHttpContextAccessor _contextAccessor;

        /// 
        /// 当前上下文
        /// 
        public static Microsoft.AspNetCore.Http.HttpContext Current => _contextAccessor.HttpContext;


        public static void Configure(IHttpContextAccessor contextAccessor)
        {
            _contextAccessor = contextAccessor;
        }
    }
}

然后在netcore web项目的Startup.cs文件中分别增加配置

        public void ConfigureServices(IServiceCollection services) 
        {
            services.AddSingleton();
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
        {            
            Web.Common.HttpContext.Configure(app.ApplicationServices.GetRequiredService());
        }

此外,.netcore中获取get查询参数方式也有区别,不再使用QueryString,而是:

var name= request.Query["name"].FirstOrDefault();

 

你可能感兴趣的:(C#,.netcore)