OData Endpoint Routing .NET Core

If you have previously used OData API in your ASP.NET Core projects, you may have been disappointed to discover that it did not support Endpoint Routing. Well, now there is good news! That has changed beginning with the release of version 7.4.0 of OData. In this tutorial, you will learn how to enable endpoint routing in your OData API applications.

Why Endpoint Routing?

Endpoint routing is a key component of the ASP.NET Core middleware model. It behaves similarly to MVC routing. It allows you to configure middleware between a UseRouting() extension method, where route decisions are made, and a UseEndpoints method, where the endpoint is executed.

Since .NET Core 3.0, Microsoft has recommended using the endpoint routing middleware for all C# web applications. They have even recommended developers to migrate existing applications to endpoint routing where possible. For this reason, it is welcome news to see the OData team proactively address the issues that prevented developers from migrating their API projects.

Enable Endpoint Routing

Previously with OData it was necessary to use MVC routing and configure the routes via a route builder. This solution was less than ideal, but it worked fine if the API was part of a separate project, because you could still take advantage of endpoint routing on your frontend web application, such as a Blazor application.

With OData 7.4.0, you no longer have to make this sacrifice. As of the time of this writing, OData 7.4.0 is in preview. To install it via Nuget, issue the following command:

Install-Package Microsoft.AspNet.OData -Version 7.4.0-beta

Next, open Startup.cs of your OData API and locate the Configure() method. This is where you will call the UseRouting and UseEndpoints middlewares.

app.UseRouting();
app.UseEndpoints()

If your application is using authentication, such as IdentityServer4, you will add the authentication and authorization middleware between these two lines, as follows:

app.UseRouting();
 
app.UseAuthentication();
app.UseAuthorization();
 
app.UseEndpoints()

Next, you must configure the OData endpoints. Expand the UseEndpoints extension method and add any OData features you wish to use. Finally, map the OData route by assigning a route prefix and configure the Entity Data Model.

app.UseEndpoints(endpoints =>
{
    endpoints.Select().Filter().OrderBy().Expand().Count().MaxTop(50);
    endpoints.MapODataRoute("api", "api", GetEdmModel());
});   

A complete configuration, may look like the following. In this example, the OData routes are located at the /api/ uri.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();
 
    app.UseAuthentication();
    app.UseAuthorization();
 
    app.UseEndpoints(endpoints =>
    {
        endpoints.Select().Filter().OrderBy().Expand().Count().MaxTop(50);
        endpoints.MapODataRoute("api", "api", GetEdmModel());
    });            
}
 
private IEdmModel GetEdmModel()
{
    var builder = new ODataConventionModelBuilder();
    builder.EntitySet<Contact>("Contacts");
    return builder.GetEdmModel();
}

That’s all! Instead of configuring OData service routes in the UseMvc extension method, you are able to configure it within the endpoints middleware. In fact, there is no longer any reason to AddMvcCore or rely on any of the underlying mvc features as of OData 7.4.0.

The Bottom Line

Endpoint routing is the future of .NET and .NET core, and it is the recommended routing model for all applications. Now, you can configure an OData API with endpoint routing. My sample Blazor Contacts template app, using EF Core and an OData API, has been updated and configured to use Endpoint Routing. Feel free to browse through the source code on GitHub to see it in action.


Was this article helpful? Share it with your friends:

Share on email
Share on facebook
Share on twitter
Share on reddit

1 thought on “OData Endpoint Routing .NET Core”

Leave a Comment

Looking for more?

You have visited our site before, and we appreciate you!

If you found the tutorials helpful, enter your email address for more free C# tips and tricks.

Can't get enough C#?

Enter your email address for more free C# tutorials and tips.