编程方式从Azure的网站内检索站点URL [英] Programmatically retrieve the site URL from inside an Azure website

查看:249
本文介绍了编程方式从Azure的网站内检索站点URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Azure的网站有湛蓝,像mysite.azurewebsites.net提供一个默认的网站网址。是否有可能从网站本身(即从ASP.NET应用程序)内得到这个网址?

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)?

有环境中的几个属性和的httpRuntime包含网站的名称(如mysite的),以便方便使用类。事情越来越复杂的时候不是默认的,但如站点的分段槽被存取(具有网站的网址像mysite-staging.azurewebsites.net)

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

推荐答案

修改(16年2月4日):您可以得到网​​址从appSetting / EnvironmentVariable websiteUrl 。这也将让你自定义的主机名,如果你有一个设置。

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.

这是有效的如果要求使用&LT击中的部位; SITENAME> .azurewebsites.net 。然后,你可以看一下 HOSTNAME 为报头中的<&站点名称GT; .azurewebsites.net

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。从 WEBSITE_SITE_NAME 环境变量

这只是给你的&LT;网站名称&GT; 一部分,所以你将不得不追加 .azurewebsites.net 部分

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。从 bindingInformation在的applicationHost.config 使用 MWA

您可以用code <一个href=\"http://blogs.msdn.com/b/carlosag/archive/2011/01/21/get-iis-bindings-at-runtime-without-being-an-administrator.aspx\"相对=nofollow>这里阅读IIS配置文件的applicationHost.config
然后读取您网站上的 bindingInformation 属性。你的函数看起来可能有点不同,像这样

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;
}

就个人而言,我会用2号

Personally, I would use number 2

这篇关于编程方式从Azure的网站内检索站点URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆