由于许多属性是由设计的,因此避免了未密封的属性,所以我正在寻找一个设置属性值的解决方案(我的第一个想法是继承这个类,并设置一个构造函数来检查web-config --使用一个密封类是不可能的):
名称空间中的ApiExplorerSettingsAttribute是System.Web.Http.Description
我希望下面的API操作隐藏在这种情况下,web-config中的值为false:
<Api.Properties.Settings>
<setting name="Hoster">
<value>False</value>
</setting>
</Api.Properties.Settings>该行动将如下所示:
[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相同的值?
发布于 2017-06-12 13:47:33
属性是静态编译到程序集中的。它们属于成员的元数据。不能在运行时更改属性。
你必须找到另一种方式来影响ApiExplorerSettings。这篇文章似乎就是你要找的:Dynamically Ignore WebAPI method on controller for api explorer documentation。
发布于 2017-11-10 08:34:07
我发现的另一个可能的解决方案是使用预处理器指令(这对我来说已经足够了,因为只有在调试时才能在swagger中看到该操作):
#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();
}https://stackoverflow.com/questions/44500951
复制相似问题