是否可以使用 asp.net 核心2,Web API,odata v4实现以下路由。
/- odata/controller/Product/ /
- odata/controller/Product/{param}/ /
- odata/controller/Product/{param}/Users /
- odata/controller/Product/{param}/Companies /
- odata/controller/Product/Companies/{param}
通过使用属性路由、声明函数和自定义路由?
在使用属性路由、函数和操作进行长时间实验后,可以使用 OData 函数实现上述目标。
下面是它的代码。
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Product");
builder.EntityType<Product>().Function("Users").Returns<Users>();
builder.EntityType<Product>().Function("Companies").Returns<Companies>();
app.UseMvc(routebuilder =>
{
routebuilder.MapODataServiceRoute( "OData", "odata", GetEdmModel());
routebuilder.EnableDependencyInjection();
}
请参阅以下内容以了解更多理解。
https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions
我也能够单独使用属性路由来实现它。
app.UseMvc(b =>
{
b.MapODataServiceRoute("odata", "api/v1", GetEdmModel());
});
在控制器中:
using Microsoft.AspNet.OData.Routing;
[EnableQuery]
[ODataRoute("content/{contentId}")]
public IActionResult Get(string contentId)