Skip to content

Commit

Permalink
Update routing sample to remove endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Dec 28, 2021
1 parent c48a776 commit 2ef9865
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions sample/ODataRoutingSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<ControllerFeatureProvider>().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)
Expand Down Expand Up @@ -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);
}
}

/// <summary>
/// My simple convention
/// </summary>
Expand Down

0 comments on commit 2ef9865

Please sign in to comment.