Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

c# - Programmatically retrieve the site URL from inside an Azure website

Azure websites have a default "site URL" provided by Azure, something like mysite.azurewebsites.net. Is it possible to get this URL from inside the website itself (i.e. from the ASP.NET application)?

There are several properties in the Environment and HttpRuntime class that contain the name of the website (e.g. "mysite") so that is easily accessible. Things are getting complicated when not the default but e.g. the staging slot of the site is accessed (that has the site URL like mysite-staging.azurewebsites.net).

So I was just wondering whether there is a straightforward way of getting this site URL directly. If not, then using one of the mentioned classes to get the site name and then somehow detecting the site slot (that could e.g. be set through a configuration value from the Azure portal) would be the solution

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit (2/4/16): You can get the URL from the appSetting/EnvironmentVariable websiteUrl. This will also give you the custom hostname if you have one setup.

There are few of ways you can do that.

1. From the HOSTNAME header

This is valid only if the request is hitting the site using <SiteName>.azurewebsites.net. Then you can just look at the HOSTNAME header for the <SiteName>.azurewebsites.net

var hostName = Request.Headers["HOSTNAME"].ToString()

2. From the WEBSITE_SITE_NAME Environment Variable

This just gives you the <SiteName> part so you will have to append the .azurewebsites.net part

var hostName = string.Format("http://{0}.azurewebsites.net", Environment.ExpandEnvironmentVariables("%WEBSITE_SITE_NAME%"));

3. From bindingInformation in applicationHost.config using MWA

you can use the code here to read the IIS config file applicationHost.config and then read the bindingInformation property on your site. Your function might look a bit different, something like this

private static string GetBindings()
{
    // Get the Site name 
    string siteName = System.Web.Hosting.HostingEnvironment.SiteName;

    // Get the sites section from the AppPool.config
    Microsoft.Web.Administration.ConfigurationSection sitesSection =
        Microsoft.Web.Administration.WebConfigurationManager.GetSection(null, null,
            "system.applicationHost/sites");

    foreach (Microsoft.Web.Administration.ConfigurationElement site in sitesSection.GetCollection())
    {
        // Find the right Site
        if (String.Equals((string) site["name"], siteName, StringComparison.OrdinalIgnoreCase))
        {

            // For each binding see if they are http based and return the port and protocol
            foreach (Microsoft.Web.Administration.ConfigurationElement binding in site.GetCollection("bindings")
                )
            {
                var bindingInfo = (string) binding["bindingInformation"];
                if (bindingInfo.IndexOf(".azurewebsites.net", StringComparison.InvariantCultureIgnoreCase) > -1)
                {
                    return bindingInfo.Split(':')[2];
                }
            }
        }
    }
    return null;
}

Personally, I would use number 2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...