15

Is it possible to get the current hostname from the controller constructor?

Both the Request and HttpContext objects are null, so Request.Url yields nothing.

public class HomeController : Controller
{
    private readonly MyEntities _entities;

    public HomeController()
    {
        //
        var hostname = Request.Url;
        if (hostname.Contains("localhost")) EFConnectionStringName="localhost";
        else EFConnectionStringName="default";
        _entities = new MyEntities(EFConnectionStringName);
    }
...

The greater problem I am trying to solve here is to choose a connection string for Entity Framework based upon the hostname. Ideas?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ben Power
  • 1,786
  • 5
  • 27
  • 35
  • See this: http://stackoverflow.com/questions/541635/how-do-i-find-fully-qualified-hostname-of-my-machine-in-c – Tomas Kubes Nov 06 '13 at 07:48
  • 1
    Taking that it says "localhost" in your code I suspect you want to use a seperate connection when you are debugging? Why not just use a web.debug.config file? See: http://blogs.msdn.com/b/webdev/archive/2009/05/04/web-deployment-web-config-transformation.aspx – mortb Nov 06 '13 at 07:51
  • 1
    There will be a few more hostnames that need to be included for different environments, maybe 6 in total. Yes, I'm aware this is not best practice, but the decision was made and I'm following orders. – Ben Power Nov 06 '13 at 10:58

4 Answers4

24

Request is indeed null during the construction of your Controller. Try this instead:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    var hostname = requestContext.HttpContext.Request.Url.Host;

    // do something based on 'hostname' value
    // ....

    base.Initialize(requestContext);
}

Also, please note that Request.Url will not return the hostname but a Uri object from which you can extract the hostname using Url.Host.

See MSDN.

haim770
  • 48,394
  • 7
  • 105
  • 133
8

Try this:

public class HomeController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        Debug.Print("Host:" + Request.Url.Host); // Accessible here
        if (Request.Url.Host == "localhost")
        {
            // Do what you want for localhost
        }
    }
}

Note, that Request.Url is an Uri object, so you should check Request.Url.Host

oxfn
  • 6,590
  • 2
  • 26
  • 34
  • Beautiful, thanks for your reply! I'm sorry I can only mark one answer as accepted. Both yours and haim770's answers did the job perfectly. – Ben Power Nov 07 '13 at 00:03
  • If someone happens to be looking a way of blocking requests based on localhost (e.g, a controller only for local tests), it is possible to write the follow to prevent actions from being executed: `filterContext.Result = RedirectToAction("Index", "Home"); Dispose(); //Important` – Reuel Ribeiro Jun 20 '16 at 12:51
0

Request URL's host doesn't necessarily have to match the hosting server name. For example if you're using DNS CNAMEs or loadbalancers.

If you'd like the machine name of the server hosting the code, try this in your controller action:

string hostingMachineName = HttpContext.ApplicationInstance.Server.MachineName;
  • Thanks that's what I'm looking for because of the load balancing scenario you mentioned, unfortunately my HttpContext doesn't have the ApplicationInstance object. What am I missing? I'm using ASP.net Core 1.1.0, is this only available in regular the .net framework? – ArgisIsland Jun 16 '17 at 16:20
0
current pc host :-
string PCName = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName;   

server host :-
String hostName = Dns.GetHostName();
chandu komati
  • 795
  • 1
  • 5
  • 21