簡體   English   中英

如何獲得WebAPI Web服務中可用方法的列表?

[英]How can I get a list of available methods in a WebAPI web service?

我正在構建一個小型測試工具,該工具應向用戶提供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()

這將返回用戶可以看到的方法列表。 意思是,他在控制器上開發並想要測試的方法。

邁克爾提到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 Blogs上有一個教程。

此外,還有其他可用的軟件包,您可以使用它們,將它們插入,從其中竊取,這些軟件包的功能類似於所需的軟件包,例如

VS Mag中有關這些的更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM