Move OData to .NET 5

Sam Xu

Sam

Introduction

Along with the Announcing .NET 5 preview 1, it’s time to move OData to .NET 5. This blog is intended to describe how easy to move the BookStore sample introduced in ASP.NET Core OData now Available  onto .NET 5.

Let’s get started.

Install .NET 5

.NET 5 SDK is required to build the .NET 5 application. So, Let’s follow up the instructions in Announcing .NET 5 preview 1 to install the .NET 5 SDK.

Meanwhile, I also install the Visual Studio 2019 Preview to edit and compile the .NET 5 project. It’s easy to download Visual Studio 2019 Preview from here. The required VS version supporting .NET 5 is 16.6.

Updating BookStore Project

It’s easy to target the BookStore project to .NET 5 when we finish the installation of .NET 5.

Just open the BookStore solution, double click the project, then edit the “BookStore.csproj” contents as below:

Updating the codes

 

In order to compile the project, we have to change some codes in the Startup.cs.

First, in ConfigureServices() method, change its content as below:

Then, in Configure() method, change its content as below:

Be noted, the parameter ‘IHostingEnvironment’ of Configure() should change to use “IWebHostEnvironment’.

 

Query the Resources

That’s all. Now, we can build and run the book store application.

For example:

We can file a GET request as : http://localhost:5001/odata/Books(2)

And the response returns the second book as follows:

The application also supports the advanced OData query options like:

http://localhost:5001/odata/Books?$filter=Price le 50&$expand=Press($select=Name)&$select=ISBN,Location($select=Street)

The response payload should look like:

Summary

Thanks for reading. We encourage you to download the latest ASP.NET Core OData package from Nuget.org and start building amazing OData service running on any .NET 5 platforms, such as Windows, MacOS and Linux.  Enjoy it!

You can refer to here for the sample project created in this blog. Any questions or concerns, feel free email to saxu@microsoft.com

 

2 comments

Comments are closed.

  • Avatar
    Shimmy Weitzhandler

    Hi Sam and thanks for your post!
    I was wondering how Restier is going to advance with these great news.
    I’m so glad the OData is coming back to life, especially seeing a significant effort in improving Microsoft.OData.Client and ODataConnectedService. For all of that, I’m very grateful!