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

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

问题描述

Azure 网站有一个由 Azure 提供的默认站点 URL",类似于 mysite.azurewebsites.net.是否可以从网站内部(即从 ASP.NET 应用程序)获取此 URL?

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

Environment 和 HttpRuntime 类中有几个属性包含网站的名称(例如mysite"),以便于访问.当不是默认值但例如时事情变得复杂访问站点的暂存槽(具有类似 mysite-staging.azurewebsites.net 的站点 URL).

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

所以我只是想知道是否有一种直接获取此站点 URL 的简单方法.如果没有,那么使用上述类之一来获取站点名称,然后以某种方式检测站点插槽(例如,可以通过 Azure 门户的配置值进行设置)将是解决方案

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

推荐答案

Edit (2/4/16): 您可以从 appSetting/EnvironmentVariable 获取 URL网站网址.如果您有一个设置,这也会为您提供自定义主机名.

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.

在请求使用 .azurewebsites.net 访问网站时有效.然后,您可以查看 .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 环境变量

这只是给你 部分,所以你必须附加 .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.从 applicationHost.config 中的 bindingInformation 使用 MWA

您可以使用代码 此处读取 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天全站免登陆