在运行时禁用OData V4元数据和控制器 [英] Disabling OData V4 meta data and controllers at runtime

查看:59
本文介绍了在运行时禁用OData V4元数据和控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们软件中有多个模块,这些模块可以作为一个产品提供.激活模块后,这些功能将可用.我们希望我们的OData API遵循相同的模式.但是我不知道如何使$ metadata忽略已禁用模块的控制器.基本上,我想确定什么时候可用,而不是应用程序启动时间.

We have several modules in our software that ship as a single product. When a module is activated, those features become available. We would like our OData APIs to follow the same pattern. However I can't figure out how to make the $metadata ignore controllers for modules that have been disabled. Basically I want to determine what is available at any time instead of application start time.

我们使用以下类型的cod来注册路线:

We are using the following type of cod to register the 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);
    }

因此,如果模块已激活,我们只希望Module1Entity显示在元数据中.我们已经有了禁用模块时禁用关联控制器的代码.

So we only want Module1Entity to show up in the metadata if the module has been activated. We already have code to disable the associated controller when the module is deactivated.

有什么想法吗?

推荐答案

我最终找到了解决方案:

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);
}

这篇关于在运行时禁用OData V4元数据和控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆