Get the computer name (HostName) and IP address (Addresses)

Source: Internet
Author: User
Tags servervariables



In the modification of the student, when the IP is really not, has been used in the previous method, appeared to show the wrong.

I have used many methods to test:

viewdata["IP"] = Request.UserHostAddress.ToString ();   Local IP dynamic get:: 1            //viewdata["IP"] = System.Web.HttpContext.Current.Request.UserHostAddress.ToString ();// Local IP dynamic get:: 1            //viewdata["IP"] = httpcontext.current.request.servervariables["REMOTE_ADDR"];//missing reference, no validation            viewdata["IP"] = Request.System.Web.HttpContext.Current.Request.UserHostAddress.ToString ();//Missing reference, no validation            viewdata["IP"] = request.servervariables["REMOTE_ADDR"]. ToString (); Local IP dynamic Get get::1</span></span>


The number I took out :1, is the representation of the local loopback address under IPv6. It is normal for you to access it using localhost. The use of IP address access or shutdown IPv6 support can not display this. Finally, I changed a way to achieve IP access to the LAN :

            Read the name of the computer          string pcname = System.Net.Dns.GetHostName ();          Get the native LAN IP address          //ipaddress localaddress = dns.gethostaddresses (name) [0];          IPAddress LocalAddress1 = Dns.gethostbyname (pcname). addresslist[0];//This sentence verification can be used          //IPAddress dynaddress = Dns.gethostbyname (pcname). ADDRESSLIST[0];

The result is:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Get computer name (HostName) and IP address (Addresses)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.