【问题标题】:Unable to read Storage account and key - Azure无法读取存储帐户和密钥 - Azure
【发布时间】:2018-10-09 18:51:16
【问题描述】:

我正在尝试将图像上传到 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 Storage Explorer”上传图片。

【问题讨论】:

  • 问题是否发生在本地?在 Azure 上?您是否检查您的配置文件或门户应用程序设置刀片中是否有这些设置?

标签: azure azure-storage azure-blob-storage


【解决方案1】:

CloudConfigurationManager.GetSetting("StorageAccountName");should work if you configure it in the Web.config file.我用 Asp.net MVC 文件测试它。它在我这边工作正常。

测试结果:

CloudConfigurationManager.GetSetting("MySetting")

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

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

【讨论】:

    【解决方案2】:

    要阅读 web.config 文件,请使用ConfigurationManager.AppSettingsCloudConfigurationManager.GetSetting()实际上是用来读取.cscfg文件的。

    【讨论】:

    • 根据我的经验,如果没有 .cscfg 文件,CloudConfigurationManager.GetSetting() 将回退并读取 app.configweb.config
    【解决方案3】:

    问题已解决。我正在使用正在查看的 web.config(这是错误的)。没有 .Net 代码更改。通过使用正确的 web.config 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 2017-05-01
      • 1970-01-01
      • 2018-01-08
      • 2019-09-17
      • 2020-11-05
      • 1970-01-01
      • 2021-05-18
      • 2018-02-27
      相关资源
      最近更新 更多