且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在.net core 2.0类库项目中使用会话

更新时间:2022-12-25 10:31:06

在项目中的任何地方使用HttpContex并从HttpContex中删除函数:

Use HttpContex and deleted functions from HttpContex anywhere in the project:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;

namespace MVC_CoreWebApp.Models
{
    public static class HttpContext2
    {
        public static HttpContext Current => new HttpContextAccessor().HttpContext;

        public static string UserHostAddress => Current.Features.Get<IHttpConnectionFeature>().RemoteIpAddress.ToString();
        public static string UserAgent => Current.Request.Headers["User-Agent"].ToString();
    }
}

在ASP.net core 2.1上成功测试

Successful testing on ASP.net core 2.1