使用 OData 和 Asp.NET Web API 自定义路由

Custom Routing with OData and Asp.NET Web API

所以我正在编写一个 API 将 return OData 但我对默认路由约定有问题。由于兼容性原因,我无法使用默认约定。

换句话说,我需要从

更改路由
/api/customers(1)/something

/api/costumers/1/something

有什么参考或想法可以帮助我吗? :)

谢谢

我认为您正在寻找的是关键段。 Web API OData 最初不支持它。参见 https://github.com/OData/WebApi/issues/105

不过,您可以编写一些代码来支持它。例如:

派生自 DefaultODataPathHandler,实现必要的功能,使 UriParser 支持 KeyAsSegment:

uriParser.UrlConventions = ODataUrlConventions.KeyAsSegment;

希望对您有所帮助。

如果您正在使用 System.Web.OData, Version=5.9.0.0,这可以通过在启动时配置您的服务时调用以下扩展方法来完成:

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);