如何获取 WebAPI Web 服务中可用方法的列表

本文关键字:方法 列表 服务 Web 何获取 获取 WebAPI | 更新日期: 2023-09-27 18:32:52

我正在构建一个小型测试工具,该工具应该为用户提供Web服务列表(使用WebAPI构建)。用户应该能够选择要测试的服务。我正在使用

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://'localhost':51062/");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

并且正在寻找类似的东西

client.GetAllWebServices()

这将返回用户可以看到的方法列表。意思是,他在控制器上开发并想要测试的方法。

如何获取 WebAPI Web 服务中可用方法的列表

迈克尔提到ApiExplorer是正确的。这为您提供了所有 WebApi 方法的详细信息。您只需要按照您想要的响应方式格式化它。

下面是一个简单的示例,用于获取所有方法及其参数和返回类型的列表。当然,您可以使其更加全面 - 只需浏览对象即可找到所需的内容:

using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Description;
namespace WebApplication1.Controllers
{
    public class ApiMethodController : ApiController
    {
        public IEnumerable<HelpMethod> GetMethods()
        {
            // get the IApiExplorer registered automatically
            IApiExplorer ex = this.Configuration.Services.GetApiExplorer();
            // loop, convert and return all descriptions 
            return ex.ApiDescriptions
                // ignore self
                .Where(d => d.ActionDescriptor.ControllerDescriptor.ControllerName != "ApiMethod")
                .Select(d =>
                {
                    // convert to a serializable structure
                    return new HelpMethod
                    {
                        Parameters = d.ParameterDescriptions.Select(p => new HelpParameter
                        {
                            Name = p.Name,
                            Type = p.ParameterDescriptor.ParameterType.FullName,
                            IsOptional = p.ParameterDescriptor.IsOptional
                        }).ToArray(),
                        Method = d.HttpMethod.ToString(),
                        RelativePath = d.RelativePath,
                        ReturnType = d.ResponseDescription.DeclaredType == null ?
                            null : d.ResponseDescription.DeclaredType.ToString()
                    };
                });
        }
    }
    public class HelpMethod
    {
        public string Method { get; set; }
        public string RelativePath { get; set; }
        public string ReturnType { get; set; }
        public IEnumerable<HelpParameter> Parameters { get; set; }
    }
    public class HelpParameter
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public bool IsOptional { get; set; }
    }
}

好处是它本身就是一个 WebApi 调用,因此您可以使用HttpClient来调用它并使用 http://www.localhost.com/api/ApiMethod/Methods 对其进行处理。下面是一个示例 JSON 响应:

[
    {
        "Method": "GET",
        "RelativePath": "api/Account/{id}",
        "ReturnType": "WebApplication1.Models.Account",
        "Parameters": [
            {
                "Name": "id",
                "Type": "System.Int32",
                "IsOptional": false
            }
        ]
    },
    {
        "Method": "POST",
        "RelativePath": "api/Account",
        "ReturnType": null,
        "Parameters": [
            {
                "Name": "a",
                "Type": "WebApplication1.Models.Account",
                "IsOptional": false
            }
        ]
    },
    {
        "Method": "GET",
        "RelativePath": "api/Maths?i={i}&j={j}",
        "ReturnType": "System.Int32",
        "Parameters": [
            {
                "Name": "i",
                "Type": "System.Int32",
                "IsOptional": false
            },
            {
                "Name": "j",
                "Type": "System.Int32",
                "IsOptional": false
            }
        ]
    }
]

展望未来

获取XML文档注释不是那么明确,但是MSDN博客上有一个教程。

此外,还有其他可用的软件包,您可以使用,挂钩,从中窃取,它们的功能类似于您需要的,例如

  • Swagger.Net
  • 拉姆

VS Mag 中有关这些的更多详细信息