我正在尝试创建一个.Net核心3.1 Odata API应用程序,该应用程序使用客户的odata路由,并希望对其使用端点路由。无论如何,我找不到通过IEndpointRouteBuilder添加客户Odata路由的方法。使用MVC和IRoutebuilder可以完成相同的操作。
这里是使用MVC和IRoutebuilder的示例。带有端点路由的3.1支持自定义OData路由吗?
ODataRoute route = routeBuilder.MapODataServiceRoute(routeName, routePrefix, builder =>
{
// Get the model from the datasource of the current request: model-per-pequest.
builder.AddService(ServiceLifetime.Scoped, sp =>
{
var serviceScope = sp.GetRequiredService<HttpRequestScope>();
// serviceScope.
string sourceString = serviceScope.HttpRequest.GetDataSource();
IEdmModel model = DataSourceProvider.GetEdmModel(sourceString);
return model;
});
// The routing conventions are registered as singleton.
builder.AddService(ServiceLifetime.Singleton, sp =>
{
IList<IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault();
routingConventions.Insert(0, new MatchAllRoutingConvention());
return routingConventions.ToList().AsEnumerable();
});
});
// route.Constraints.
IRouter customRouter = routeBuilder.ServiceProvider.GetService<IRouter>();
// Get constraint resolver.
IInlineConstraintResolver inlineConstraintResolver = routeBuilder.ServiceProvider.GetRequiredService<IInlineConstraintResolver>();
CustomODataRoute odataRoute = new CustomODataRoute(customRouter != null ? customRouter : routeBuilder.DefaultHandler,
routeName,
routePrefix,
new CustomODataPathRouteConstraint(routeName),
inlineConstraintResolver);
routeBuilder.Routes.Remove(route);
routeBuilder.Routes.Add(odataRoute);