简体   繁体   中英

Unable to read Storage account and key - Azure

I'm trying to upload image to Blob. Hence in the Web.Config I have settings like this:

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

Here is how it looks like in the portal:

在此处输入图片说明

My Dot Net code is as below:

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

However, my "account" and "key" variable are returning NULL.

Could any one please suggest, what I'm doing wrong? Is there anything that I'm missing?

Note: I'm able to upload image via "Azure Storage Explorer".

CloudConfigurationManager.GetSetting("StorageAccountName"); should work If you config it in the Web.config file. I test it with Asp.net MVC file. It works correctly on my side.

在此处输入图片说明

Test Result:

在此处输入图片说明

CloudConfigurationManager.GetSetting("MySetting")

Which will read values from all the configuration files ie, app.config, web.config and ServiceConfiguration.cscfg.

Note: If we config the appsetting in the Web.Debug.config not web.config file,it will get null value.

To read web.config file, please use ConfigurationManager.AppSettings . CloudConfigurationManager.GetSetting() is used to read .cscfg file actually.

Issue is resolved. I was using web.config that is under view (which is wrong). No .Net code changed. By using correct web.config solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM