gpt4 book ai didi

c# - OData 返回 'Cannot find the services container for route ' odata-Unversioned'

转载 作者:行者123 更新时间:2023-12-04 10:42:00 29 4
gpt4 key购买 nike

我正在尝试使用 OData 设置新的 .NET Core 3.1 API。

我在 2.2 中有一个可以使用 OData 的 .NET Core API,所以我要放弃那个。

我正在使用三个特定于 OData 的 nuget 包:Microsoft.AspNetCore.OData v7.3.0Microsoft.AspNetCore.OData.Versioning v4.1.1Microsoft .AspNetCore.OData.Versioning.ApiExplorer v4.1.1

当我向我的 Controller 发送 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/ .我什至还尝试完全按照博客文章中的设置进行设置,但是如果没有版本控制,我什至无法让 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();
}
}

这是 Controller

[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() 方法中的 Controller 名称与 Controller 类的实际名称相差一个“s”。

关于c# - OData 返回 'Cannot find the services container for route ' odata-Unversioned',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59881062/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com