ApiExplorer:如何更改IgnoreApi的默认值

问题描述 投票:-1回答:1

我们有几个ApiController实现,我们不希望大多数操作都包含在ApiExplorer的元数据中。

默认情况下,如果您没有将[ApiExplorerSettings(IgnoreApi = true)]添加到您的操作中,它将被添加,这意味着默认值为false。

这可能是由于IgnoreApi是一个布尔值并默认为false但是如何在不必覆盖true的情况下将此默认值更改为ApiExplorerSettings

这是一个不使用MVC组件的基本Web Api实现。

我试着寻找简单的基于配置的解决方案或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而不是标记我不想使用的操作。

c# asp.net-web-api asp.net-apicontroller
1个回答
0
投票

对于那些感兴趣的人,我最终覆盖了ApiExplorer类来覆盖ShouldExploreAction和ShouldExploreController方法。我在那里颠倒了布尔逻辑,它按照要求工作。

© www.soinside.com 2019 - 2024. All rights reserved.