From 2ef9865e423009100d74c652334070885e77faea Mon Sep 17 00:00:00 2001 From: Sam Xu Date: Mon, 27 Dec 2021 21:23:33 -0800 Subject: [PATCH] Update routing sample to remove endpoint --- sample/ODataRoutingSample/Startup.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sample/ODataRoutingSample/Startup.cs b/sample/ODataRoutingSample/Startup.cs index 5bb6c0d4d..43a4dab2f 100644 --- a/sample/ODataRoutingSample/Startup.cs +++ b/sample/ODataRoutingSample/Startup.cs @@ -18,6 +18,9 @@ using Microsoft.AspNetCore.OData.Batch; using Microsoft.OData; using ODataRoutingSample.Models; +using Microsoft.AspNetCore.Mvc.Controllers; +using System.Reflection; +using System.Linq; namespace ODataRoutingSample { @@ -61,6 +64,16 @@ public void ConfigureServices(IServiceCollection services) IEdmModel model3 = EdmModelBuilder.GetEdmModelV3(); services.AddControllers() + /* If you want to remove $metadata endpoint, you can use ControllerFeatureProvider as follows + .ConfigureApplicationPartManager(manager => + { + manager.FeatureProviders.Remove(manager.FeatureProviders.OfType().FirstOrDefault()); + manager.FeatureProviders.Add(new RemoveMetadataControllerFeatureProvider()); + }) + + or, remove MetadataRoutingConvention in AddOData as + opt.Conventions.Remove(opt.Conventions.First(convention => convention is MetadataRoutingConvention)); + */ .AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(5) .AddRouteComponents(model0) .AddRouteComponents("v1", model1) @@ -121,6 +134,19 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } } + public class RemoveMetadataControllerFeatureProvider : ControllerFeatureProvider + { + protected override bool IsController(TypeInfo typeInfo) + { + if (typeInfo.FullName == "Microsoft.AspNetCore.OData.Routing.Controllers.MetadataController") + { + return false; + } + + return base.IsController(typeInfo); + } + } + /// /// My simple convention ///