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

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

问题描述

使用.net Core 1.1和Microsoft.AspNetCore.OData库,我能够获得一个OData端点,该端点与我的简单控制器一起执行get,$ expand和其他查询.但是,我无法让它返回要返回的$ metadata.这个问题( $ Metadata与WebAPi OData属性路由不起作用 )是针对相同的问题,但是自此发布以来,.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软件包获得了更大的成功.引用示例,元数据和默认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天全站免登陆