【发布时间】:2019-04-24 12:52:01
【问题描述】:
我们有几个ApiController 实现,我们不希望大多数操作包含在 ApiExplorer 的元数据中。
默认情况下,如果您不将[ApiExplorerSettings(IgnoreApi = true)] 添加到您的操作中,它将被添加,因此这意味着默认值为 false。
这可能是因为IgnoreApi 是一个布尔值并默认为false,但我如何才能将此默认值更改为true 而不必覆盖ApiExplorerSettings?
这是一个基本的 WebApi 实现,不使用 MVC 组件。
我尝试寻找基于简单配置的解决方案或ApiExplorerSettings 用法示例,但没有一个真正适合我。
最接近我想要的是:DotNetCore - is ApiExplorer supported, and how to use it?;但是,它侧重于 MVC。
// For example
[RoutePrefix("api/test")]
public class TestController : ApiController
{
[HttpGet]
[Route("helloworld")]
[ApiExplorerSettings(IgnoreApi = false)]
public string HelloWorld() {
return "Hello world!";
}
[HttpGet]
[Route("goodbyeworld")]
[ApiExplorerSettings(IgnoreApi = true)]
public string HelloWorld() {
return "Goodbye world!";
}
[HttpGet]
[Route("hiworld")]
[ApiExplorerSettings(IgnoreApi = true)]
public string HelloWorld() {
return "Hi world!";
}
[HttpGet]
[Route("seeyaworld")]
[ApiExplorerSettings(IgnoreApi = true)]
public string HelloWorld() {
return "See ya world!";
}
}
我希望能够只在我想使用的操作上使用ApiExplorerSettings,而不是标记我不想使用的操作。
【问题讨论】:
-
一个选项:创建一个常量。将属性值设置为该常量。
-
@Amy 这可行,但我们想使用或重用已经是 .NET 框架一部分的东西。
-
常量已经是语言的一部分......
标签: c# asp.net-web-api asp.net-apicontroller