5

After updating my project to .NET6 and OData to 8.0.4, a new Metadata controller with these endpoints appeared:

enter image description here

I want to disable it somehow or remove it from my service.

Code for adding OData service:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddControllers(mvcOptions => mvcOptions.EnableEndpointRouting = false)
        .AddOData(opt => opt.AddRouteComponents("", GetEdmModel()).Select().Expand());
}

Versions:

  • .NET6
  • ASP.NET Core 6
  • OData 8.0.4
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
kanils_
  • 385
  • 1
  • 17

2 Answers2

7

There are several options to solve this:

  1. You can use the Controller feature provider to exclude MetadataController
  2. You can use Conventions in ODataOptions to remove MetadataRoutingConvention.
  3. You can implement DocumentFilter to hide MetadataController and related schemas from the Swagger

#2 is the simplest way:

services
    .AddControllers()
    .AddOData(opt =>
   {
         opt.Conventions.Remove(opt.Conventions.OfType<MetadataRoutingConvention>().First());
         opt.AddRouteComponents("", GetEdmModel()).Select().Expand();
    });

#3 If you just want to hide MetadataController and related schemas from the Swagger:

public class SwaggerODataControllerDocumentFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        // remove controller
        foreach (ApiDescription apiDescription in context.ApiDescriptions)
        {
            var actionDescriptor = (ControllerActionDescriptor)apiDescription.ActionDescriptor;
            if (actionDescriptor.ControllerName == "Metadata")
            {
                swaggerDoc.Paths.Remove($"/{apiDescription.RelativePath}");
            }
        }

        // remove schemas
        foreach ((string key, _) in swaggerDoc.Components.Schemas)
        {
            if (key.Contains("Edm") || key.Contains("OData"))
            {
                swaggerDoc.Components.Schemas.Remove(key);
            }
        }
    }
}

Add it to the services.AddSwaggerGen:

cfg.DocumentFilter<SwaggerODataControllerDocumentFilter>();

For option #1 you can do like:

public class RemoveMetadataControllerFeatureProvider : ControllerFeatureProvider
    {
        protected override bool IsController(TypeInfo typeInfo)
        {
            if (typeInfo.FullName == "Microsoft.AspNetCore.OData.Routing.Controllers.MetadataController")
            {
                // or just compare the name
                return false;
            }

            return base.IsController(typeInfo);
        }
    }

In the startup.cs

services.AddControllers()
    .ConfigureApplicationPartManager(manager =>
     {
       manager.FeatureProviders.Remove(manager.FeatureProviders.OfType<ControllerFeatureProvider>().FirstOrDefault());
          manager.FeatureProviders.Add(new RemoveMetadataControllerFeatureProvider());
     }).AddOData(....)
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
1

I recently faced the similar problem. I get the solution by

  • removing odata service injection from startup file (because swagger automatically add metadata classes if I tried to inject odata from startup)
  • downgrading OData from 8.0.4 to 8.0.0
  • and implementing ODataQueryOptions inside controller instead of EnableQuery actionfilter
  • used ODataBuilder/OdataModelBuilder within controller instead of startup. in my case it is ODataBuilder

enter image description here