OData 返回“找不到路由 'odata-Unversioned' 的服务容器
OData returning 'Cannot find the services container for route 'odata-Unversioned'
我正在尝试使用 OData 设置新的 .NET Core 3.1 API。
我有一个 .NET Core API 在 2.2 中使用 OData,所以我要放弃那个。
我正在使用三个特定于 OData 的 nuget 包:Microsoft.AspNetCore.OData v7.3.0、Microsoft.AspNetCore.OData.Versioning v4.1.1 和 Microsoft.AspNetCore.OData.Versioning.ApiExplorer v4.1.1。
当我向我的控制器发送 GET 请求时,我不断收到以下响应:
System.InvalidOperationException: Cannot find the services container for route 'odata-Unversioned'. This should not happen and represents a bug.
at Microsoft.AspNet.OData.PerRouteContainerBase.GetODataRootContainer(String routeName)
at Microsoft.AspNet.OData.Extensions.HttpRequestExtensions.CreateRequestScope(HttpRequest request, String routeName)
at Microsoft.AspNet.OData.Extensions.HttpRequestExtensions.CreateRequestContainer(HttpRequest request, String routeName)
at Microsoft.AspNet.OData.Routing.ODataPathRouteConstraint.<>c__DisplayClass0_0.<Match>b__0()
at Microsoft.AspNet.OData.Routing.ODataPathRouteConstraint.GetODataPath(String oDataPathString, String uriPathString, String queryString, Func`1 requestContainerFactory)
at Microsoft.AspNet.OData.Routing.ODataPathRouteConstraint.Match(HttpContext httpContext, IRouter route, String routeKey, RouteValueDictionary values, RouteDirection routeDirection)
at Microsoft.AspNet.OData.Routing.UnversionedODataPathRouteConstraint.Match(HttpContext httpContext, IRouter route, String routeKey, RouteValueDictionary values, RouteDirection routeDirection)
at Microsoft.AspNetCore.Routing.RouteConstraintMatcher.Match(IDictionary`2 constraints, RouteValueDictionary routeValues, HttpContext httpContext, IRouter route, RouteDirection routeDirection, ILogger logger)
at Microsoft.AspNetCore.Routing.RouteBase.RouteAsync(RouteContext context)
at Microsoft.AspNetCore.Routing.RouteCollection.RouteAsync(RouteContext context)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
请注意,我要求 API 需要进行版本控制。我不确定版本控制是否与此错误有关,但我使用的旧 .NET Core 2.2 API 也使用 OData 版本控制。有谁知道路线的服务容器是什么?
我设置我的项目类似于 https://devblogs.microsoft.com/odata/experimenting-with-odata-in-asp-net-core-3-1/。
我什至还尝试完全按照博客 post 中的设置进行设置,但是如果没有版本控制,我什至无法让 OData 工作。当我尝试删除版本控制时,我一直收到 404 并且它无法到达路线。我的路由设置是否正确?
这里是 ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// Add API versioning
services.AddApiVersioning(options => options.ReportApiVersions = true);
// Add OData
services.AddOData().EnableApiVersioning();
services.AddODataApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
// Add support for controllers and API-related features
services.AddControllers(mvcOptions => mvcOptions.EnableEndpointRouting = false);
}
这里是配置
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
var apiVersion = new ApiVersion(1, 0);
app.UseMvc(routeBuilder =>
{
routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
app.UseMvc(routes =>
routes.MapVersionedODataRoute("odata", "api/v{version:apiVersion}", app.GetEdmModel(), apiVersion));
});
}
这里是扩展方法GetEdmModel()
我上面用的
public static class ODataExtensions
{
public static IEdmModel GetEdmModel(this IApplicationBuilder app)
{
var model = GetEdmModel(app.ApplicationServices);
return model;
}
private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
var builder = new ODataConventionModelBuilder(serviceProvider);
// Entities
builder.EntityType<Object>().HasKey(x => x.Id);
// Controllers
builder.EntitySet<Object>("Object");
return builder.GetEdmModel();
}
}
这是控制器
[Route("odata")]
[ApiVersion("1")]
public class ObjectsController : ODataController
{
private readonly ILogicService _logicService;
public AlternateIdsController(ILogicService logicService)
{
_logicService = logicService;
}
[HttpGet]
[EnableQuery]
public IActionResult Get()
{
return Ok(_logicService.Browse());
}
}
GetEdmModel() 方法中的控制器名称与控制器的实际名称class 相差一个's'。
我可以确认这实际上是一个错误,但它 只有 似乎在为不存在的实体请求 URL 时出现。这被跟踪为 Issue 553,即将修复。
我正在尝试使用 OData 设置新的 .NET Core 3.1 API。
我有一个 .NET Core API 在 2.2 中使用 OData,所以我要放弃那个。
我正在使用三个特定于 OData 的 nuget 包:Microsoft.AspNetCore.OData v7.3.0、Microsoft.AspNetCore.OData.Versioning v4.1.1 和 Microsoft.AspNetCore.OData.Versioning.ApiExplorer v4.1.1。
当我向我的控制器发送 GET 请求时,我不断收到以下响应:
System.InvalidOperationException: Cannot find the services container for route 'odata-Unversioned'. This should not happen and represents a bug.
at Microsoft.AspNet.OData.PerRouteContainerBase.GetODataRootContainer(String routeName)
at Microsoft.AspNet.OData.Extensions.HttpRequestExtensions.CreateRequestScope(HttpRequest request, String routeName)
at Microsoft.AspNet.OData.Extensions.HttpRequestExtensions.CreateRequestContainer(HttpRequest request, String routeName)
at Microsoft.AspNet.OData.Routing.ODataPathRouteConstraint.<>c__DisplayClass0_0.<Match>b__0()
at Microsoft.AspNet.OData.Routing.ODataPathRouteConstraint.GetODataPath(String oDataPathString, String uriPathString, String queryString, Func`1 requestContainerFactory)
at Microsoft.AspNet.OData.Routing.ODataPathRouteConstraint.Match(HttpContext httpContext, IRouter route, String routeKey, RouteValueDictionary values, RouteDirection routeDirection)
at Microsoft.AspNet.OData.Routing.UnversionedODataPathRouteConstraint.Match(HttpContext httpContext, IRouter route, String routeKey, RouteValueDictionary values, RouteDirection routeDirection)
at Microsoft.AspNetCore.Routing.RouteConstraintMatcher.Match(IDictionary`2 constraints, RouteValueDictionary routeValues, HttpContext httpContext, IRouter route, RouteDirection routeDirection, ILogger logger)
at Microsoft.AspNetCore.Routing.RouteBase.RouteAsync(RouteContext context)
at Microsoft.AspNetCore.Routing.RouteCollection.RouteAsync(RouteContext context)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
请注意,我要求 API 需要进行版本控制。我不确定版本控制是否与此错误有关,但我使用的旧 .NET Core 2.2 API 也使用 OData 版本控制。有谁知道路线的服务容器是什么?
我设置我的项目类似于 https://devblogs.microsoft.com/odata/experimenting-with-odata-in-asp-net-core-3-1/。 我什至还尝试完全按照博客 post 中的设置进行设置,但是如果没有版本控制,我什至无法让 OData 工作。当我尝试删除版本控制时,我一直收到 404 并且它无法到达路线。我的路由设置是否正确?
这里是 ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// Add API versioning
services.AddApiVersioning(options => options.ReportApiVersions = true);
// Add OData
services.AddOData().EnableApiVersioning();
services.AddODataApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
// Add support for controllers and API-related features
services.AddControllers(mvcOptions => mvcOptions.EnableEndpointRouting = false);
}
这里是配置
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
var apiVersion = new ApiVersion(1, 0);
app.UseMvc(routeBuilder =>
{
routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
app.UseMvc(routes =>
routes.MapVersionedODataRoute("odata", "api/v{version:apiVersion}", app.GetEdmModel(), apiVersion));
});
}
这里是扩展方法GetEdmModel()
我上面用的
public static class ODataExtensions
{
public static IEdmModel GetEdmModel(this IApplicationBuilder app)
{
var model = GetEdmModel(app.ApplicationServices);
return model;
}
private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
var builder = new ODataConventionModelBuilder(serviceProvider);
// Entities
builder.EntityType<Object>().HasKey(x => x.Id);
// Controllers
builder.EntitySet<Object>("Object");
return builder.GetEdmModel();
}
}
这是控制器
[Route("odata")]
[ApiVersion("1")]
public class ObjectsController : ODataController
{
private readonly ILogicService _logicService;
public AlternateIdsController(ILogicService logicService)
{
_logicService = logicService;
}
[HttpGet]
[EnableQuery]
public IActionResult Get()
{
return Ok(_logicService.Browse());
}
}
GetEdmModel() 方法中的控制器名称与控制器的实际名称class 相差一个's'。
我可以确认这实际上是一个错误,但它 只有 似乎在为不存在的实体请求 URL 时出现。这被跟踪为 Issue 553,即将修复。