Disabling OData V4 Metadata and Controllers at Run Time

We have several modules in our software that come as a single product. When the module is activated, these functions become available. We would like our OData APIs to execute the same pattern. However, I cannot figure out how to make the $ metadata controls ignore modules that were disabled. Basically, I want to determine what is available at any time, not the time the application starts.

We use the following type of cod to register routes:

static public void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); var builder = new ODataConventionModelBuilder(); builder.EntitySet<Module1Entity>("Module1Entities"); builder.EntitySet<Module2Entity>("Module2Entities"); config.MapODataServiceRoute("odata", "api", builder.GetEdmModel()); } protected void Application_Start(object sender, EventArgs e) { GlobalConfiguration.Configure(Register); } 

Therefore, we need the 1Entity module to appear in metadata if the module has been activated. We already have code to disable the associated controller when the module is deactivated.

Any ideas?

+5
source share
1 answer

I ended up finding a solution:

 static public void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); var builder = new ODataConventionModelBuilder(); if (IsModule1Enabled) { builder.EntitySet<Module1Entity>("Module1Entities"); { if (IsModule2Enabled) { builder.EntitySet<Module2Entity>("Module2Entities"); } var conventions = ODataRoutingConventions.CreateDefault(); conventions.Insert(0, new MyAttributeRoutingConvention("odata", config)); config.MapODataServiceRoute("odata", "api", builder.GetEdmModel(), new DefaultODataPathHandler(), conventions); } public class MyAttributeRoutingConvention : AttributeRoutingConvention { public MyAttributeRoutingConvention(string routeName, HttpConfiguration configuration) : base(routeName, configuration) { } public override bool ShouldMapController(HttpControllerDescriptor controller) { if (controller.ControllerType == typeof(Module1EntitiesController)) { return IsModule1Enabled; } if (controller.ControllerType == typeof(Module2EntitiesController)) { return IsModule2Enabled; } return base.ShouldMapController(controller); } } protected void Application_Start(object sender, EventArgs e) { GlobalConfiguration.Configure(Register); } 
+1
source

Source: https://habr.com/ru/post/1274941/


All Articles