繁体   English   中英

将 Azure blob 存储与 .NET 结合使用

[英]Using Azure blob storage with .NET

嗨,我正在尝试学习如何使用 Azure 和 .Net。

我构建了一个简单的“Hello World”webApp 并创建了一个 Blob 存储,其中包含一个包含 3 个电子邮件地址的表(当然,webApp 和 Blob 存储在同一订阅下)

我想要做的就是在运行网站时显示这 3 个电子邮件地址。 即从 blob 存储中的表中获取此地址列表并将其呈现在网站上。

我不知道从哪里开始,微软的教程也没有多大帮助。

在我的 WebApp 下的解决方案资源管理器中,有一个文件夹“Controllers”。 我的直觉是在那里开设一个新课程,但我又不知道如何开始。

如果您遵循此文档,这将非常简单。

我有一个演示如下:

1.在visual studio -> Nuget Package Manager,安装最新版本的WindowsAzure.ConfigurationManagerWindowsAzure.Storage

2.在 web.config -> appsettings 节点中,添加<add key="StorageConnectionString" value="your storage account string" /> 在此处输入图片说明

3.在你的asp.net web项目中,添加一个派生自TableEntity的自定义类:

public class CustomerEntity : TableEntity
{
    public CustomerEntity(string lastName, string firstName)
    {
        this.PartitionKey = lastName;
        this.RowKey = firstName;
    }

    public CustomerEntity() { }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }
}

4.然后在您的控制器中,添加以下代码(我在 Contact 方法中添加了代码):

       using System.Web.Mvc;

       using Microsoft.Azure;

       using Microsoft.WindowsAzure.Storage;

       using Microsoft.WindowsAzure.Storage.Table; 

        public ActionResult Contact()
        {
            //define the emails to output in web page
            string emails = "";

            // Retrieve the storage account from the connection string.

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(

                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the table client.

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object that represents the "people" table.

            CloudTable table = tableClient.GetTableReference("people");

            TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>();

            foreach (CustomerEntity entity in table.ExecuteQuery(query))
            {

                    emails += entity.Email+";";

            }

            ViewBag.Message = "Your contact page."+emails;

            return View();
        }
  1. 我的天蓝色表和测试结果: 在此处输入图片说明

所有电子邮件地址都显示在网页中: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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