首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态设置ApiExplorerSettingsAttributes值

动态设置ApiExplorerSettingsAttributes值
EN

Stack Overflow用户
提问于 2017-06-12 13:42:31
回答 2查看 2.6K关注 0票数 3

由于许多属性是由设计的,因此避免了未密封的属性,所以我正在寻找一个设置属性值的解决方案(我的第一个想法是继承这个类,并设置一个构造函数来检查web-config --使用一个密封类是不可能的):

名称空间中的ApiExplorerSettingsAttributeSystem.Web.Http.Description

我希望下面的API操作隐藏在这种情况下,web-config中的值为false:

代码语言:javascript
复制
<Api.Properties.Settings>
  <setting name="Hoster">
    <value>False</value>
  </setting>
</Api.Properties.Settings>

该行动将如下所示:

代码语言:javascript
复制
[HttpGet, Route("api/bdlg")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(BdlgDataStorage))]
[ApiExplorerSettings(IgnoreApi = Properties.Settings.Default.Hoster)]
private async Task<BdlgDataStorage> GetBdlgStorageValues()
{
    using (var context = new BdlgContext())
        return context.BdlgDataStorages
            .Include(s=>s.ChangeTrack)
            .Where(w=>w.Isle > 56)
            .Select(selectorFunction)
            .ToListAsync();
}

重要的是:

[ApiExplorerSettings(IgnoreApi = Properties.Settings.Default.Hoster)]

在这里,我得到一个编译器错误:

属性参数必须是属性参数类型的常量表达式、类型表达式或数组创建表达式。

有谁知道,我如何将IgnoreApi的值设置为与web相同的值?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-12 13:47:33

属性是静态编译到程序集中的。它们属于成员的元数据。不能在运行时更改属性。

你必须找到另一种方式来影响ApiExplorerSettings。这篇文章似乎就是你要找的:Dynamically Ignore WebAPI method on controller for api explorer documentation

票数 4
EN

Stack Overflow用户

发布于 2017-11-10 08:34:07

我发现的另一个可能的解决方案是使用预处理器指令(这对我来说已经足够了,因为只有在调试时才能在swagger中看到该操作):

代码语言:javascript
复制
#if DEBUG
    [ApiExplorerSettings(IgnoreApi = false)]
#else
    [ApiExplorerSettings(IgnoreApi = true)]
#endif
    [SwaggerResponse(HttpStatusCode.OK, Type = typeof(BdlgDataStorage))]
    [HttpGet, Route("api/bdlg")]
    private async Task<BdlgDataStorage> GetBdlgStorageValues()
    {
        using (var context = new BdlgContext())
            return context.BdlgDataStorages
                .Include(s=>s.ChangeTrack)
                .Where(w=>w.Isle > 56)
                .Select(selectorFunction)
                .ToListAsync();
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44500951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档