-1

Hi I am trying to learn how to work with Azure and .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)

All I want to do is to show this 3 email addresses when I run the website. i.e take this list of addresses from the table in the blob storage and render it over the website.

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". My intuition is to open a new class there but again I have no clue of how to start.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
jrz
  • 1,213
  • 4
  • 20
  • 54
  • So what is the thing you want to address, how to access table storage or how to add a new controller? Don't over complicate things in one step. – Peter Bons Nov 04 '18 at 11:34
  • Lets start with how to access table storage. Actually I want to access and transfer a certain column to a list. – jrz Nov 04 '18 at 11:44
  • There are numerous examples here: https://learn.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-dotnet – Peter Bons Nov 04 '18 at 11:54
  • Table Storage is different from Blob Storage, which could be part of why this is confusing you. And as @PeterBons points out, there's a considerable amount of documentation and tutorials available. You're also asking about multiple things (e.g. you're asking how to build a Controller in a Web App, which has nothing to do with Table or Blob Storage). Unfortunately, as written, this is off-topic for a few reasons: *too broad* (lots of things here, lots of ways to solve these challenges), *unclear* (can't tell specifically what problem you're having), and *documentation request* (tutorials, etc). – David Makogon Nov 04 '18 at 13:22
  • @l.zvi, does the answer below works for you? – Ivan Glasenberg Nov 06 '18 at 08:45
  • @IvanYang Amazing! thank you so much – jrz Nov 08 '18 at 09:27
  • @IvanYang Though I am wondering why I do not see Web.config in my app ..? – jrz Nov 08 '18 at 09:48
  • what's the type of your project? web.config only exists in a web project. – Ivan Glasenberg Nov 08 '18 at 09:49
  • it's a .net core web application, and it uses the .json(appsettings.json) instead of .config file. If you uses the .net framework web application, you can see the web.config file. – Ivan Glasenberg Nov 08 '18 at 09:55
  • @IvanYang Thanks. let me ask 2 questions. 1. What should I have done differently to get a project with Web.config? 2. How do I implement step #2 in your answer if I chose to stay with the .net core web application ? – jrz Nov 08 '18 at 09:59
  • @IvanYang thanks just did. – jrz Nov 08 '18 at 10:04
  • .net core is cross-platform, but .net framework does not. For details, you can refer to this [link](https://stackoverflow.com/questions/38063837/whats-the-difference-between-net-core-net-framework-and-xamarin). For your 2nd question, I can answer it tomorrow, because it's time to go home:) – Ivan Glasenberg Nov 08 '18 at 10:06
  • @IvanYang thank you. Looking forward for your answer tomorrow. – jrz Nov 08 '18 at 10:12

1 Answers1

1

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

2.In the web.config -> appsettings node, add <add key="StorageConnectionString" value="your storage account string" />: enter image description here

3.In your asp.net web project, add a custom class derived from 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):

       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: enter image description here

all the email addresses are displayed in web page: enter image description here

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • ViewBag and View do not exist in the current context – jrz Nov 08 '18 at 15:21
  • @l.zvi, because you use the asp.net core project, some thing changes. I can do a demo in .net core later. But it will be 2 days later, I have something emergency today. – Ivan Glasenberg Nov 09 '18 at 02:58