首页 > 解决方案 > 无法读取存储帐户和密钥 - Azure

问题描述

我正在尝试将图像上传到 Blob。因此在 Web.Config 我有这样的设置:

            <appSettings>
                <add key="webpages:Enabled" value="false" />
                <add key="StorageAccountName" value="storageaccount1"/> 
                <add key="StorageAccountKey" value="storageaccountkey1"/>
              </appSettings>

以下是它在门户中的样子:

在此处输入图像描述

我的点网代码如下:

        public class ConnectionString
            {
                static string account = CloudConfigurationManager.GetSetting("StorageAccountName");
                static string key = CloudConfigurationManager.GetSetting("StorageAccountKey");
                public static CloudStorageAccount GetConnectionString()
                {
                    string connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account, key);
                    return CloudStorageAccount.Parse(connectionString);
                }
            }

但是,我的“帐户”和“密钥”变量返回 NULL。

任何人都可以建议,我做错了什么?有什么我想念的吗?

注意:我可以通过“Azure 存储资源管理器”上传图像。

标签: azureazure-storageazure-blob-storage

解决方案


CloudConfigurationManager.GetSetting("StorageAccountName");如果你在Web.config文件中配置它应该可以工作。我用 Asp.net MVC 文件测试它。它在我这边正常工作。

在此处输入图像描述

测试结果:

在此处输入图像描述

CloudConfigurationManager.GetSetting("MySetting")

它将从所有配置文件(即 app.config、web.config 和 ServiceConfiguration.cscfg)中读取值。

注意:如果我们在Web.Debug.config而不是 web.config 文件中配置 appsetting,它将得到 null 值。


推荐阅读