C# 获取 服务器IP、客户端IP


C#获取服务IP

string GetAddressIP()
        {
            ///获取服务端
            string AddressIP = string.Empty;
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAddress.ToString();
                }
            }
            return AddressIP;

}
View Code

C#获取客户端IP

        /// <summary>
        /// 获取操作用户IP地址
        /// </summary>
        /// <returns>IP地址</returns>
        public static string GetClientIP()
        {
            //获取本地的IP地址
            string userHostAddress = HttpContext.Current.Request.UserHostAddress;
 
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
 
            //最后判断获取是否成功,并检查IP地址的格式
            if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
            {
                return userHostAddress;
            }
            return "127.0.0.1";
        }
 
        /// <summary>
        /// 检查IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
View CRequest.ApplicationPath  -------->  Request.PhysicalPath      -------->   D:\Projects\Solution\web\News\Press\Content.aspx

System.IO.Path.GetDirectoryName(Request.PhysicalPath)     -------->   D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath                                              -------->  D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath)             --------> Content.aspx
Request.CurrentExecutionFilePath                                          -------->  /News/Press/Content.aspx
Request.FilePath                                                                      --------> /News/Press/Content.aspx
Request.Path                                                                           --------> /News/Press/Content.aspx/123
Request.RawUrl                                                                      -------->  /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath                                                       --------> /News/Press/Content.aspx/123
Request.Url.AbsoluteUri                                                          --------> http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme                                                               -------->  http
Request.Url.Host                                                                    --------> localhost
Request.Url.Port                                                                     -------->  1897
Request.Url.Authority                                                             --------> localhost:1897
Request.Url.LocalPath                                                          --------> /News/Press/Content.aspx/123
Request.PathInfo                                                                  --------> /123
Request.Url.PathAndQuery                                                  --------> /News/Press/Content.aspx/123?id=1
Request.Url.Query                                                                --------> ?id=1
Request.Url.Fragment  
Request.Url.Segments                                                         -------->  /、News/、Press/、Content.aspx/、123


获取整个url地址:

在页面(cstml)中 

Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(Context.Request);

在 Controller 中

Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(Request);

获取请求的方式(scheme:http/https):http

In asp.net 4.6 -> Request.Url.Scheme

in .net core -> Context.Request.Scheme (cshtml) , in Controller -> Request.Scheme

获取域名(不带端口号)[Get the host]:

In asp.net 4.6 -> Request.Url.Host

in .net core -> Context.Request.Host.Host (cshtml) , in Controller -> Request.Host.Host

获取域名(带端口号)[Get the host]: localhost:4800

In asp.net 4.6 ->

in .net core -> Context.Request.Host.Value (cshtml) , in Controller -> Request.Host.Value

获取路径(Get the path): /account/login

In asp.net 4.6:

In .net core: @Context.Request.Path (cshtml)

获取端口号(Get port): 4800 (if a url contains port)

In asp.net 4.6: Request.Url.Port

In .net core: @Context.Request.Host.Port (cshtml) , in Controller -> Request.Host.Port






免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM