ASP Web API POST JSON

Prevouisly on Serversncode we created a WEB API that created Data into a Azure Table storage. This had a POST method that accepts the parameters, ASP WEB API.

But what if we wanted to pass the data in as part of a JSON. Well lets do that. Source code is here

First up lets create our Player Class, Last Time we created the PlayerEntity class this is for our table Storage but we don't want to pass that around, we want a player class so we can pass that into the POST method.

In Models create a new Class Player

public class Player
{
    public string Sport_VC { get; set; }
    public string Club_VC { get; set; }
    public string First_Name_VC { get; set; }
    public string Last_Name_VC { get; set; }
    public string Position_VC { get; set; }
}

To make a clear break from this work and the last post we're going to create a new WebAPI Controller called PlayerJController.

    [HttpPost]
    public string Post([FromBody] Player record)
    {
        string sResponse = "";

        // Create the entity with a partition key for sport and a row
        // Row should be unique within that partition
        PlayerEntity _record = new PlayerEntity(record.Sport_VC, sRow);

        _record.Sport_VC = record.Sport_VC;
        _record.First_Name_VC = record.First_Name_VC;
        _record.Last_Name_VC = record.Last_Name_VC;
        _record.Club_VC = record.Club_VC;
        _record.Position_VC = record.Position_VC;

        return sResponse;
    }

I've cleaned the code above of the work we do to create the Azure table etc. Just to keep it simple.

.NET will handle the heavy lifting for us. If we open Postman now and create a new POST call to our new API method.

Test API

  • Change it to a Post Method
  • Select Body, and RAW type out the JSON body
  • Change the Application type to "JSON (application/json)
  • Enter the URL localhost:999090/API/PlayerJ

That's it when you run that it will send the JSON into the API method and you can work with it to create the player entity and store it in Azure Table storage.

Prevouisly on Serversncode we created a WEB API that created Data into a Azure Table storage. This had a POST method that accepts the parameters, ASP WEB API. But what if we wanted to pass the data in as part of a JSON. Well lets do that. Source code is…

Read More

ASP Web API with Azure Table Storage

Using ASP Web API with Azure table storage

We've already covered the basics of creating an ASP Web API project and the basics for Azure Table Storage

ASP WEB API intro

Azure Table Storage

For the sample code for this check out our GitHub

Now we're going to get into using both of these. Creating a simple ASP Web API to create data into an Azure Table storage and to get that data back out. We'll do some testing of the API with Postman

As earlier we're using Visual Studio 2015.

Lets get going

Create a Web API project

Create Web API Project

Add Azure Storage from nuget.

We'll create a new controller building on our last post on Azure Table storage. We'll go for a "PlayerController"

Create API Controller

Select "Web API 2 Controller" from the Add scaffold option.

In the model create an entity for Table Storage "PlayerEntity". The player entity will look the same as we had in our last post.

Player Entity

Now back to our controller, we want to create two methods a POST method to create a player and a Get method to get players for a sport.

    public List<PlayerEntity> Get(string sport)
    {
        List<PlayerEntity> _records = new List<PlayerEntity>();

        //Create a storage account object.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Retrieve a reference to the table.
        CloudTable table = tableClient.GetTableReference("player");

        // Create the table if it doesn't exist.
        table.CreateIfNotExists();

        // Get All Players for a sport
        TableQuery<PlayerEntity> query = new TableQuery<PlayerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, sport));
        
        foreach (PlayerEntity entity in table.ExecuteQuery(query))
        {
            _records.Add(entity);
        }

        return _records;
    }

This is the same as we used in our last post on Azure table storage.

We create a storage account with a connection string.

The table client is created and this we check for the table "Player" if it doesn't exist we create it.

Azure Table storage is a partitioned database. For this demo we create a partition based on Sport. So we can create players for "Soccer", "Rugby" and "Hurling" and so on. The get method will then only return the players for the sport we want.

For the Post there are a few ways to pass parameters we'll keep it simple for now. We'll pass the parameters into the POST method.

    // POST api/Player
    public string Post(string sSport, string sRow, string sFirstName, string sLastName, string sClub, string sPostition)
    {
        string sResponse = "";

        // Create our player

        // Create the entity with a partition key for sport and a row
        // Row should be unique within that partition
        PlayerEntity _record = new PlayerEntity(sSport, sRow);

        _record.Sport_VC = sSport;
        _record.First_Name_VC = sFirstName;
        _record.Last_Name_VC = sLastName;
        _record.Club_VC = sClub;
        _record.Position_VC = sPostition;

        //Create a storage account object.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Retrieve a reference to the table.
        CloudTable table = tableClient.GetTableReference("player");

        // Create the TableOperation object that inserts the customer entity.
        TableOperation insertOperation = TableOperation.Insert(_record);

        try
        {
            // Execute the insert operation.
            table.Execute(insertOperation);

            sResponse = "OK";
        }
        catch (Exception ex)
        {
            sResponse = "Failed: " + ex.ToString();
        }
        return sResponse;
    }

With this Post method, we can take the parameters passed in and then create a player in our Table storage.

Testing with PostMan

We will use Postman to do the testing if you don't have it go get it and check out this post on using Postman

Our Get method is simple it takes a parameter of sport and then will return all the players in that sport.

In post man we create a GET Request to

http://*LOCALHOST*/api/Player?sport=soccer

This will return a list of all players in Soccer.

To test the POST method change the method in Postman to "POST"

http://*LOCALHOST*/api/Player?sSport=soccer&sRow=52&sFirstName=Chris&sLastName=Smalling&sClub=Man Utd&sPostition=CB

That's it, since we kept our POST simple we just need to pass the parameters and we can add the player to the Azure table.

Coming soon we'll start to work on our POST method and passing the parameters in different ways.

For the sample code for this check out our GitHub

Using ASP Web API with Azure table storage We've already covered the basics of creating an ASP Web API project and the basics for Azure Table Storage ASP WEB API intro Azure Table Storage For the sample code for this check out our GitHub Now we're going to get into…

Read More

Azure Table Storage

About Azure Table Storage

Windows Azure Table storage is a service that stores structured data in the cloud. It's like NoSQL but with more of the SQL, for example it does not store the data as a Document but as rows. You still get the flexibility of NoSQL storage.

Table storage is a non-relational, key-value-pair, storage system suitable for storing massive amounts of unstructured data. Whereas relational stores such as SQL Server, with highly normalized designs, are optimized for storing data so that queries are easy to produce, the non-relational stores like Table Storage are optimized for simple retrieval and fast inserts. Table storage is schemaless, it's easy to adapt your data as the needs of your application evolve. Access to data is fast for all kinds of applications. Table storage is typically significantly lower in cost than traditional SQL for similar volumes of data.

You can use Table storage to store flexible datasets, such as user data for web applications, address books, device information, and any other type of metadata that your service requires. You can store any number of entities in a table, and a storage account may contain any number of tables, up to the capacity limit of the storage account.

Pricing

Windows Azure table storage is cheap. All you pay for the number of transactions and the amount of storage you need. So no licences, no servers, no VM's just what you need when you need it.

Azure Table pricing

This is the pricing in EUR for North Europe for more go here and select "Table" as the storage to see the pricing.

So what that means is that if I want to store 100 GB of data and access it 200,000 times. I pay less than EUR 6. That's great however you look at it. It means I can store a massive about of data and have quick ready access to it and not have to worry about the cost.

Getting started

To get started, open your Azure Protal and select "New" filter to "Storage account" and select "General Purpose" provide a name and select the "Resource Group"

Create Azure Storage Group

Once you hit create you have to wait for it to deploy and create the storage account.

Azure Storage Group Deployed

Once it's deployed go ahead and open it up. Selecting "Tables" will show "no tables found" in the tables blade.

We need the Access keys for be able to do anything. In the main storage blade select "Settings" and click to open Access keys this will show you a "Key1" and "Key2"

Azure Storage Access keys

We'll need them next.

Do some code

For the sample code github

In Visual studio create a new Console Application

Install the Azure Storage nuget package

Now in the main cs file lets bring in the Azure Storage

using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Table;

For Azure Storage our connection string looks like this

DefaultEndpointsProtocol=https;AccountName=storagesample;AccountKey=storagesamplekey

Where StorageSample and StorageSampleKey would be replaced with the account name and the key that you want to use.

For this demo I created a static string and loaded my connection string into it.

Next we need to create our entity. Create a new class 'PlayerEntity'

In this class we'll turn it into a Table Entity

using Microsoft.WindowsAzure.Storage.Table;

public class PlayerEntity : TableEntity

The rest of our Player class should come together like this

public class PlayerEntity : TableEntity
{
    public PlayerEntity(string skey, string srow)
    {
        this.PartitionKey = skey;
        this.RowKey = srow;
    }

    public PlayerEntity() { }

    public string Sport_VC { get; set; }
    public string Club_VC { get; set; }
    public string First_Name_VC { get; set; }
    public string Last_Name_VC { get; set; }
    public string Position_VC { get; set; }
}

Back over to our Program.cs, lets add some code to do something.

    static void Main(string[] args)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);
        
        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Retrieve a reference to the table.
        CloudTable table = tableClient.GetTableReference("player");

        // Create the table if it doesn't exist.
        table.CreateIfNotExists();

        // Get All Players for a sport

        TableQuery<PlayerEntity> query = new TableQuery<PlayerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "soccer"));

int iCount = 0;
        foreach (PlayerEntity entity in table.ExecuteQuery(query))
        {
            Console.WriteLine("Club:" + entity.Club_VC + " Player Name: " + entity.First_Name_VC + " " + entity.Last_Name_VC + " Position: " + entity.Position_VC);

            iCount++;
        }



    }

Lets walk through this code step by step.

        //Create a storage account object.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);

This will create the storage account with our connection string.

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Retrieve a reference to the table.
        CloudTable table = tableClient.GetTableReference("player");

        // Create the table if it doesn't exist.
        table.CreateIfNotExists();

Create a table client and then we create a table called "player" then we check if it exists. If it does not then table.CreateIfNotExists() will create a new table for us.

Next we'll go and get some data from the table.

        // Get All Players for a sport

        TableQuery<PlayerEntity> query = new TableQuery<PlayerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "soccer"));

You might have worked out the theme for this now. But this code allows us to create a query to get the players for a sport. In this case "soccer", The partition we created on this table is for Sports. Once we have built the query we can execute it.

int iCount = 0;
        foreach (PlayerEntity entity in table.ExecuteQuery(query))
        {
            Console.WriteLine("Club:" + entity.Club_VC + " Player Name: " + entity.First_Name_VC + " " + entity.Last_Name_VC + " Position: " + entity.Position_VC);

            iCount++;
        }

Here we execute the query which will return records for the Sport and then write them to the console. If you run this application now it will return nothing because we haven't created any data.

So lets create some data
static Boolean CreateData(string sSport, string sRow, string sFirstName, string sLastName, string sClub, string sPostition)
{
Boolean bSuccess = false;

        // Create our player
       
        // Create the entity with a partition key for sport and a row
        // Row should be unique within that partition
        PlayerEntity _record = new PlayerEntity(sSport, sRow);

        _record.Sport_VC = sSport;
        _record.First_Name_VC = sFirstName;
        _record.Last_Name_VC = sLastName;
        _record.Club_VC = sClub;
        _record.Position_VC = sPostition;
        
        //Create a storage account object.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Retrieve a reference to the table.
        CloudTable table = tableClient.GetTableReference("player");


        // Create the TableOperation object that inserts the customer entity.
        TableOperation insertOperation = TableOperation.Insert(_record);

        try
        {

            // Execute the insert operation.
            table.Execute(insertOperation);

            bSuccess = true;
        }
        catch (Exception ex)
        {
            bSuccess = false;
        }


        return bSuccess;
    }

What does this code do,

First of all we create a new method for creating the data.

        // Create our player
       
        // Create the entity with a partition key for sport and a row
        // Row should be unique within that partition
        PlayerEntity _record = new PlayerEntity(sSport, sRow);

        _record.Sport_VC = sSport;
        _record.First_Name_VC = sFirstName;
        _record.Last_Name_VC = sLastName;
        _record.Club_VC = sClub;
        _record.Position_VC = sPostition;

We create our player Entity and setup the values.

PlayerEntity _record = new PlayerEntity(sSport, sRow)

Will create our table entity for us with the Row ID and the Partition key.
Row ID should be unqiue

TableOperation insertOperation = TableOperation.Insert(_record);

This sets it all ready to go, then we do the table.Execute(insertOperation); to create the record on our table.

So what I've done for this demo is add a iCount int that will hold the record count. Then at the end we add a call to our create data and it will create our first player.

CreateData("soccer", iCount.ToString(), "Wayne", "Rooney", "Man Utd", "Striker");

Once you run the app it will create the footballers and that's it, it's very simple to get started.

For the sample code github

About Azure Table Storage Windows Azure Table storage is a service that stores structured data in the cloud. It's like NoSQL but with more of the SQL, for example it does not store the data as a Document but as rows. You still get the flexibility of NoSQL storage. Table…

Read More