问题描述
我正在尝试将 Azure 网站连接到 Azure blob(我打算在容器中托管一些文件,然后从我的网站中获取它们).
我从本教程开始:
我还尝试直接将 StorageConnectionString
的值(使用我的帐户名和密钥)而不是在以下语句中引用它:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key"].ConnectionString);
我仍然遇到同样的错误.我似乎无法在互联网上找到任何东西.有什么想法吗?
谢谢!
你的代码有一个根本性的错误.
首先你设置一个 AppSetting:
<配置><应用设置><添加键=存储连接字符串"value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key"/></app设置></配置>
然后你尝试获取连接字符串:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
这根本行不通.设置 AppSetting 时,需要阅读 AppSetting.设置 ConnectionString 时,需要读取 Connection String.
因此,解决方案是保持 web.config 不变,并将获取存储帐户的行更改为:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
或保留连接字符串的行,但将 web.config 更改为:
<配置><连接字符串><添加名称="StorageConnectionString"connectionString="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" providerName="System.Data.SqlClient"/></connectionStrings></配置>
当然,您必须为云存储帐户和存储帐户密钥输入您的真实值(account-name
根本不起作用).
I'm trying to connect an Azure website to an Azure blob (where I intend to host some files in a container and then grab them from my website).
I started out with this tutorial: http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/
I deployed my website, and then started following this tutorial: http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#setup-connection-string
Since I was using an Azure website, I added the following code to my web.config file (under the WebApplication1 project). There is also a web.config file under the Views folder but I didn't modify that.
<configuration>
<appSettings>
<add key="StorageConnectionString"
value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
</appSettings>
</configuration>
I followed all the steps in the tutorial and installed the relevant NuGet packages and then included these namespaces in my Controllers/HomeController.cs
file:
using System.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
Then I added the following statement in the ActionResult Index()
method (which runs by default).
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
When I try and run the solution, I now get this error:
I also tried directly putting the value of the StorageConnectionString
(with my account name and key) instead of referencing to it in the following statement:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key"].ConnectionString);
I still get the same error. I can't seem to find anything on the internet. Any ideas?
Thanks!
You have a fundamental mistake in your code.
First you set an AppSetting:
<configuration>
<appSettings>
<add key="StorageConnectionString"
value="DefaultEndpointsProtocol=https;AccountName=account- name;AccountKey=account-key" />
</appSettings>
</configuration>
Then you try to get a connection string:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
This will simply not work. When set AppSetting, you need to read AppSetting. When you set ConnectionString, you need to read Connection String.
So, the solution to be just keep the web.config as is, and change the line where you get storage account to:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
Or keep your line for Connection strings but change web.config to:
<configuration>
<connectionStrings>
<add name="StorageConnectionString"
connectionString="DefaultEndpointsProtocol=https;AccountName=account- name;AccountKey=account-key" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
And of course, you have to put your real values for Cloud Storage account and Storage Account key (account-name
will simply never work).
这篇关于将 Azure Blob 与 Azure 网站连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!