ApiExplorer:如何更改IgnoreApi的默认值 [英] ApiExplorer: How to Change The Default for IgnoreApi

查看:148
本文介绍了ApiExplorer:如何更改IgnoreApi的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a couple of ApiController implementations and we do not want most operations to be included in the metadata of ApiExplorer.

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

By default, if you do not add [ApiExplorerSettings(IgnoreApi = true)] to your operation, it will be added so this means the default is false.

这可能是由于 IgnoreApi 是布尔值,默认为 false ,但是我如何才能将此默认值更改为 true ,而不必覆盖 ApiExplorerSettings ?

This probably due to IgnoreApi being a boolean and defaulting to false but how can I change this default to true without having to override ApiExplorerSettings?

这是不使用MVC组件的基本WebApi实现.

This is a basic WebApi implementation without using MVC components.

我尝试寻找基于简单配置的解决方案或 ApiExplorerSettings 用法的示例,但没有一个真正适合我.

I tried looking around for simple config based solutions or examples of ApiExplorerSettings usage but none have really worked out for me.

最接近我想要的是: DotNetCore-是支持ApiExplorer,以及如何使用它?;但是,它专注于MVC.

The closest to what I want is: DotNetCore - is ApiExplorer supported, and how to use it?; however, it focuses on 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 ,而不是标记不想使用的操作.

I want to be able to just use ApiExplorerSettings on operations which I want to use instead of marking the ones I do not want to use.

推荐答案

对于那些感兴趣的人,我最终重写了ApiExplorer类,以重写ShouldExploreAction和ShouldExploreController方法.我在那里反转了布尔逻辑,并且按要求工作.

For those who are interested, I ended up overriding the ApiExplorer class to override the ShouldExploreAction and ShouldExploreController methods. I reversed the boolean logic there and it works as requested.

[根据要求编辑并带有示例]

您可以执行以下操作:

using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Description;
using System.Web.Http.Routing;

namespace WebApi.Api
{
    public class CustomApiExplorer : ApiExplorer
    {
        public CustomApiExplorer(HttpConfiguration configuration) : base(configuration)
        {
        }

        /// <summary>
        /// Determines whether the controller should be considered.
        /// </summary>
        /// <param name="controllerVariableValue">The controller route parameter value.</param>
        /// <param name="controllerDescriptor">The associated <see cref="HttpControllerDescriptor">controller descriptor</see>.</param>
        /// <param name="route">The associated <see cref="IHttpRoute">route</see>.</param>
        /// <returns>True if the controller should be explored; otherwise, false.</returns>
        public override bool ShouldExploreController(string controllerVariableValue, HttpControllerDescriptor controllerDescriptor, IHttpRoute route)
        {
            if (string.IsNullOrEmpty(controllerVariableValue) || controllerDescriptor == null || route == null)
            {
                throw new ArgumentException();
            }

            var setting = controllerDescriptor.GetCustomAttributes<ApiExplorerSettingsAttribute>().FirstOrDefault();

            // Basically you check if there is a setting used and if ignore is set to true or false. You can also check if the routing is as one would expect but that is a different discussion. With this the ApiExplorer changes its logic by only registering API's that actively state IgnoreApi = false.
            if (setting != null && !setting.IgnoreApi)
            {
                return true;
            }

            return false;
        }
    }
}

在WebApiConfig中使用自定义类

在WebApiConfig.cs文件中,可以通过将以下行放在Register(HttpConfiguration config)方法中来覆盖IApiExplorer服务实例.

Use custom class in WebApiConfig

In the WebApiConfig.cs file you can overide the IApiExplorer service instance by placing the following line in the Register(HttpConfiguration config) method.

public static void Register(HttpConfiguration config) {
    ...
    config.Services.Replace(typeof(IApiExplorer), new CustomApiExplorer(config));
    ...
}

这篇关于ApiExplorer:如何更改IgnoreApi的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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