Matheus da Rosa
Matheus da Rosa

Reputation: 13

Custom Routing with OData and Asp.NET Web API

So I am writing an API that will return OData but I have a problem with the default routing convention. Due compatibility reasons I can not use the default convention.

In other words, I need to change the routing from

/api/customers(1)/something

to

/api/costumers/1/something

Any reference or idea to help me? :)

Thanks

Upvotes: 1

Views: 2328

Answers (2)

Andacious
Andacious

Reputation: 1172

If you are using System.Web.OData, Version=5.9.0.0, this can be done by invoking the following extension method when configuring your service on startup:

using System.Web.OData.Extensions;
...
// This is just to show the type, this will likely come from the web app startup
HttpConfiguration config = new HttpConfiguration(); 
...
config.SetUrlConventions(ODataUrlConventions.KeyAsSegment);

Upvotes: 0

Sam Xu
Sam Xu

Reputation: 3380

I think what you are looking is key-as-segment. It's not original supported in Web API OData. See https://github.com/OData/WebApi/issues/105

However, you can code a little bit to support it. For example:

Derived from DefaultODataPathHandler, implement the necessary function, enable the UriParser to support KeyAsSegment:

uriParser.UrlConventions = ODataUrlConventions.KeyAsSegment;

Hope it can help you.

Upvotes: 2

Related Questions