简体   繁体   English

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

[英]Using Azure blob storage with .NET

Hi I am trying to learn how to work with Azure and .Net .嗨,我正在尝试学习如何使用 Azure 和 .Net。

I built a simple "Hello World" webApp and created a Blob Storage with a table that has 3 email addresses (the webApp and the blob storage are under the same subscription of course)我构建了一个简单的“Hello World”webApp 并创建了一个 Blob 存储,其中包含一个包含 3 个电子邮件地址的表(当然,webApp 和 Blob 存储在同一订阅下)

All I want to do is to show this 3 email addresses when I run the website.我想要做的就是在运行网站时显示这 3 个电子邮件地址。 ie take this list of addresses from the table in the blob storage and render it over the website.即从 blob 存储中的表中获取此地址列表并将其呈现在网站上。

I have no idea where to start and the Microsoft tutorials did not help much.我不知道从哪里开始,微软的教程也没有多大帮助。

Inside my solution explorer under the WebApp, there is a folder "Controllers".在我的 WebApp 下的解决方案资源管理器中,有一个文件夹“Controllers”。 My intuition is to open a new class there but again I have no clue of how to start.我的直觉是在那里开设一个新课程,但我又不知道如何开始。

It's very simple if you follow this doc .如果您遵循此文档,这将非常简单。

I have a demo as below:我有一个演示如下:

1.In visual studio -> Nuget Package Manager, install the latest version of WindowsAzure.ConfigurationManager and WindowsAzure.Storage 1.在visual studio -> Nuget Package Manager,安装最新版本的WindowsAzure.ConfigurationManagerWindowsAzure.Storage

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

3.In your asp.net web project, add a custom class derived from TableEntity: 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.Then in your controller, add the following code(I add the code in Contact method): 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. My azure table and the test result:我的天蓝色表和测试结果: 在此处输入图片说明

all the email addresses are displayed in web page:所有电子邮件地址都显示在网页中: 在此处输入图片说明

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

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