Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData v4 on .Net Core 1.1 missing /$metadata

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?

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"
    }
  ]
}

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)

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

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();
});
like image 776
me_online Avatar asked Dec 31 '25 08:12

me_online


1 Answers

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());
        });
    }
like image 172
jamesf Avatar answered Jan 01 '26 21:01

jamesf