In this post, we will customize an Microsoft Docs sample to create an OData version 4 service with data and publish it to a new App Service in Azure. This is useful if you want to test OData integrations such as virtual tables in the Microsoft Power Platform, which we will do in the next post. Note this sample won’t support a lot of OData features, it is more intended to give you a high level service you can use to test certain OData features. I will provide a more detailed OData service at some point.
In this sample, we will create a service that returns a list of products.
First, create a new Visual Studio ASP.NET Web Application project:
Select Empty, and Web API:
Install Microsoft.AspNet.OData and its dependencies from Nuget:
Add a new Model class called Product.cs:
Add code for the class with 2 attributes, ID and Name. We will deviate from the sample and make the ID a GUID:
Add a new class to Controllers called ProductsController.cs:
This creates the class like below:
We will change this and use the sample code as a base to do the following. We will add the using references required, and create a method that returns a list of sample data. The Id is a guid:
Next, let’s change the code in App_Start WebApiConfig.cs from below:
To:
Now build the project. Note, if you created the project using SSL, you may want to turn it off in the project properties:
Now when we run the project, and browse to /products, we see the data returned:
Now let’s deploy to Azure:
Select Azure:
Select Azure App Service:
Click Create:
And Finish:
This will create the resources in Azure. The final step is to publish the code to the new app service:
In https://portal.azure.com you will see the new app service:
We now see the code up in Azure:
Browsing out to https://carlodatav4sample.azurewebsites.net/$metadata, we see the metadata for the service:
Now, let’s see if OData commands such as $select and $filter work. We can see below our queries are not actually working.
A $select example is https://carlodatav4sample.azurewebsites.net/products?$select=Name (however, this still returns the Id field as well):
A $filter example is https://carlodatav4sample.azurewebsites.net/products?$filter=Name eq ‘Item1’ (however this still returns all records):
Let’s change the code to support querying.
First, we will change the Products controller to use the EnableQuery attribute and return List<Product>:
Next, we will add to to the WebApiConfig class:
config.AddODataQueryFilter();
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
Now let’s compile and build. We see the queries are working:
$select
$filter
Now what if we want to select a single product. We can do this using the notation below, where the guid is the id of the product:
https://localhost:44303/products(ad725636-7d7a-4809-aded-eaf593ba8882)
However, we need to add some code to make this happen.
We will add to our controller:
Now when we run the query, we see our single product is returned:
I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.
IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.
THANK YOU, AND LET’S KEEP LEARNING TOGETHER.
CARL