Udostępnij za pośrednictwem


One thing you must do when: Request.Url.Host is returning an unexpected result

 

Scenario: You are using Request.Url.Host property in your web application. You are expecting it to return the friendly host name but it is returning the NetBIOS name of server instead, or vice-versa, there are many possible combination on how this could go wrong. But the bottom line is, Request.Url.host property is not returning the expected results.

In this situation, you must check how UseHostName/SetHostName (for IIS 6.0) properties are configured. These IIS properties really affects what Request.Url.Host will return.

In such situations, you can reliably use SetHostname to return the desired result (NetBIOS, FQDN, Host name etc.) for any specific website.

This is how it works. Here are major players who will come into the equation.

UseHostName

SetHostName

HTTP_HOST

SERVER_NAME

The spec for UseHostName and SetHostName says

The UseHostName property will instruct IIS to always replace the SERVER_NAME variable with the fully qualified machine name.

Use the SetHostName property if you must specify a host name that is different from the computer name.

It also says if you SetHostName then IIS will ignore UseHostName property. So essentially SetHostName is actually defining what will be returned. This is clear enough. If you enable UseHostName for a website, it will return whatever value in SERVER_NAME server variable. If you configure SetHostName, whatever value you assign to this property, will be returned by that specific site. 

Now looking at the code snippet for Request.Url

 if (this._url == null)
{
    string serverName = this._wr.GetServerName();
    if ((serverName.IndexOf(':') >= 0) && (serverName[0] != '['))
    {
        serverName = "[" + serverName + "]";
    }
    this._url = new Uri(this._wr.GetProtocol() + "://" + serverName + ":" + this._wr.GetLocalPortAsString() 
       +  this.Path + queryStringText);
}

And then looking what GetServerName() is doing for the worker request

 public override string GetServerName()
{
    return this.GetServerVariable("SERVER_NAME");
}

 

So we can see that it is also fetching the value from SERVER_NAME server variable. Now combining this information with the behaviour of UseHostName/SetHostName properties, we can safely ensure that if our web

application is returning the desired result or not in terms of NetBIOS, FQDN, Public Host Header etc. If you run few tests, you will see that these properties are actually modifying SERVER_NAME variable which in terns is affecting what is returned by Request.Url.Host. Also please note this behaviour is true for IIS 6.0. On IIS 7x servers, these properties are replaced by alternateHostName which works in a bit different way.This is how you can set/modify these two properties Ref: This KB

To set the UseHostName property, follow these steps:

  1. Click Start, click Run, type cmd, and then click OK to open a command prompt.

  2. Change to the folder where the Adsutil.vbs tool is located. By default, this folder is the following:

    %SYSTEMROOT%\Inetpub\AdminScripts

  3. Type the following command, where x is your site identifier:

    cscript adsutil.vbs set w3svc/x/UseHostName true

To set the SetHostName property, follow these steps:

  1. Click Start, click Run, type cmd, and then click OK to open a command prompt.

  2. Change to the folder where the Adsutil.vbs tool is located. By default, this folder is the following:

    %SYSTEMROOT%\Inetpub\AdminScripts

  3. Type the following command, where x is your site identifier and hostname is the alternate host name that you want to use:

    cscript adsutil.vbs set w3svc/x/SetHostName hostname

Hope this helps!