簡體   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