Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

847
Views
How to hide OData Metadata Controller in Swagger?

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
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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(....)
over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!