Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OData返回';找不到路由';奥达塔大学';_C#_.net Core_Odata_.net Core 3.1 - Fatal编程技术网

C# OData返回';找不到路由';奥达塔大学';

C# OData返回';找不到路由';奥达塔大学';,c#,.net-core,odata,.net-core-3.1,C#,.net Core,Odata,.net Core 3.1,我正在尝试使用OData设置一个新的.NET Core 3.1 API 在2.2版本中,我有一个使用OData的.NET核心API,所以我将不再使用它 我正在使用三个特定于OData的nuget软件包:Microsoft.AspNetCore.OData v7.3.0、Microsoft.AspNetCore.OData.Versioning v4.1.1和Microsoft.AspNetCore.OData.Versioning.ApiExplorer v4.1.1 当我向我的控制器发送GET

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

在2.2版本中,我有一个使用OData的.NET核心API,所以我将不再使用它

我正在使用三个特定于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)
下面是配置

// 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();
    }
}

GetEdmModel()方法中的控制器名称与控制器类的实际名称相差“s”

我可以确认这实际上是一个bug,但它似乎只有在为不存在的实体请求URL时才会出现。正在跟踪此问题,正如即将进行的修复一样。

不幸的是,名称不匹配将导致此错误出现。很高兴你找到了罪犯。当将来有人犯了这个错误时,修复程序有望提供更好的体验。
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());
    }
}