OData Endpoint Routing .NET Core
Tags: webdev, api, csharp, dotnet
Published: 2020-03-10 12:29:10

---
title: OData Endpoint Routing .NET Core
published: true
date: 2020-03-09 05:00:00 UTC
tags: webdev, api, csharp, dotnet
canonical_url: https://wellsb.com/csharp/aspnet/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](https://wellsb.com/csharp/aspnet/odata-api-ef-core-blazor/) 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.

```csharp
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:

```csharp
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.

```csharp
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.

```csharp
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](https://github.com/bradwellsb/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](https://github.com/bradwellsb/blazor-contacts) to see it in action.

[Source](https://wellsb.com/csharp/aspnet/odata-endpoint-routing-net-core/)