Get hostname from MVC

1

I have a form that every time a record is inserted or modified it adds the hostname in an SQL field, the detail that when I pass the system to the server where it would be hosted, it puts me the hostname of the server. Could I somehow get the hostname of the machine that is using the system and this save? Currently, I get the hostname from the code in c #

 string computer_name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");

Any ideas?

    
asked by Richard 19.05.2018 в 01:13
source

1 answer

3

You can use the following HttpRequest.UserHostAddress and HttpRequest.UserHostName to get both the client IP and the name of the client machine. An example code

using System;
using System.Web.Mvc;

namespace Mvc.Controllers
{
    public class HomeController : ClientController
    {
        public ActionResult Index()
        {
            string ip = Request.UserHostAddress;
            string hostName = Request.UserHostName;

            ...
        }
    }
}
    
answered by 19.05.2018 / 01:36
source