简体   繁体   English

如何在asp.net核心的静态类中获取远程IP?

[英]how to get remote IP in a static class in asp.net core?

such as the question,because static class not IHttpContextAccessor and HttpContext property,how to solve it?如题,因为静态类没有IHttpContextAccessor和HttpContext属性,怎么解决?

public static class IPHelper
{
    //this is a wrong define
    private readonly IHttpContextAccessor _accessor;

    public string GetRemoteIP()
    {
        return _accessor.HttpContext.Connection.RemoteIpAddress?.ToString();
    }
}

above,_accessor cannot define in static class and not construct.上面,_accessor 不能在静态类中定义,也不能构造。 how to injection it?怎么注射呢?

使用abstract关键字而不是static

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM