.Net Core 1.1 上的 OData v4 丢失/$metadata [英] OData v4 on .Net Core 1.1 missing /$metadata

查看:26
本文介绍了.Net Core 1.1 上的 OData v4 丢失/$metadata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .net Core 1.1 和 Microsoft.AspNetCore.OData 库,我能够让 OData 端点与我的简单控制器一起工作以执行 get、$expand 和其他查询.但是,我无法让它返回要返回的 $metadata.这个问题($Metadata with WebAPi OData Attribute Routing Not Working) 是针对相同的问题,但是自发布以来 .Net API 已更改.

Using .net Core 1.1, with the Microsoft.AspNetCore.OData libraries, I am able to get an OData endpoint working with my simple controller to perform get, $expand, and other queries. However, I can't get it to return the $metadata to be returned. This question ($Metadata with WebAPi OData Attribute Routing Not Working) is for the same problem, however the .Net APIs have changed since this was posted.

是否需要启用设置、标志或其他内容?

Is there a setting, flag, or something else I need to enable?

这个(http://localhost:52315/odata)好像返回了元数据,>

This (http://localhost:52315/odata) seems to return the meta data,

{
  "@odata.context":"http://localhost:52315/odata/$metadata","value":[
    {
      "name":"Users","kind":"EntitySet","url":"Users"
    },{
      "name":"HelloComplexWorld","kind":"FunctionImport","url":"HelloComplexWorld"
    }
  ]
}

这个(http://localhost:52315/odata/$metadata)给了我错误:

this (http://localhost:52315/odata/$metadata) gives me the error:

An unhandled exception occurred while processing the request.
NotSupportedException: No action match template '$metadata'
in 'MetadataController'

Microsoft.AspNetCore.OData.Routing.Conventions.DefaultODataRoutingConvention.SelectAction(RouteContext routeContext)

我的 Startup.cs 如下所示:

My Startup.cs looks like this:

public void Configure(IApplicationBuilder app) {
   app.UseDeveloperExceptionPage();
   app.UseOData("odata");
   app.UseMvcWithDefaultRoute();
}

public void ConfigureServices(IServiceCollection services) {
    services.AddMvc().AddWebApiConventions();
    services.AddSingleton<ISampleService, ApplicationDbContext>();
    services.AddOData<ISampleService>(builder =>
    {
         builder.Namespace = "Sample";
         builder.EntityType<ApplicationUser>();
         builder.EntityType<Product>();
         builder.Function("HelloComplexWorld").Returns<Permissions>();
    });
}

注意:我可以通过在我的 ConfigureServices(...) 方法的开头添加它来解决这个问题,尽管鉴于 $metadata 支持应该是核心平台的一部分,这似乎是错误的.

NOTE: I can work around it by adding this at the start of my ConfigureServices(...) method, though it seems wrong given $metadata support should be part of the core platform.

app.Use(async (context, next) => {
     if (0 == string.Compare(context.Request.Path, @"/odata/$metadata", true)) {
        context.Request.Path = "/odata";
   }
   await next.Invoke();
});

推荐答案

我今天重新审视了这个问题,并且使用 Microsoft.AspNetCore.OData.vNext 6.0.2-alpha-rtm 包取得了更大的成功.参考 this 示例,元数据和默认 OData 路由按预期工作.我的最低配置是:

I revisited this today and had more success using the Microsoft.AspNetCore.OData.vNext 6.0.2-alpha-rtm package. Referencing this example, the metadata and default OData routing worked as expected. My minimal configuration is:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOData();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        var modelBuilder = new ODataConventionModelBuilder();
        modelBuilder.EntitySet<Document>("Documents");

        app.UseMvc(builder =>
        {
            builder.MapODataRoute("odata", modelBuilder.GetEdmModel());
        });
    }

这篇关于.Net Core 1.1 上的 OData v4 丢失/$metadata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆