我怎样才能得到一个的WebAPI Web服务的方法列表? [英] How can I get a list of available methods in a WebAPI web service?

查看:201
本文介绍了我怎样才能得到一个的WebAPI Web服务的方法列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个小的测试工具,它应该提供给用户的Web服务的列表(使用的WebAPI建)。用户应该能够选择一个服务来测试。
我使用

I'm building a small test tool that should provide the user a list of web services (built using WebAPI). The user should be able to choose a service to test. I'm using

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()

这将返回的方法列表用户可以看到的。这意味着,他在控制器上开发的,并希望测试的方法。

which would return a list of methods that the user can see. Meaning, the methods that he developed on the controller and wants to test.

推荐答案

迈克尔是正确提的 ApiExplorer 。这给你所有的WebAPI方法您的详细信息。你只需要你想怎么应对格式化。

Michael was correct to mention ApiExplorer. This gives you details of all the WebApi methods for you. You just need to format it how you want the response.

下面是一个简单的例子来获取所有与参数方法的列表和返回类型。当然,你可以让这个更全面的 - 只是浏览的对象找到你所需要的:

Here is a simple example to get a list of all the methods with their parameters and return types. You can of course make this much more comprehensive - just browse the objects to find what you need:

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响应:

The nice thing is that it is a WebApi call itself, so you can use the HttpClient to call and process it using http://www.localhost.com/api/ApiMethod/Methods. Here's a sample JSON response:

[
    {
        "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文档注释掉不是那么明确的,但有一个教程< A HREF =http://blogs.msdn.com/b/yaohuang1/archive/2012/12/10/asp-net-web-api-help-page-part-3-advanced-help-page-customizations。 ASPX> MSDN博客。

Getting XML doc comments out isn't so clear cut, but there is a tutorial on MSDN Blogs.

此外,还有其他的封装,您可以使用,钩到,从,它做同样的你需要什么偷例如

Also, there are other packages available which you can use, hook into, steal from, which do similar to what you need, for example

  • Swagger.Net
  • RAML

在VS马格这些更多细节

这篇关于我怎样才能得到一个的WebAPI Web服务的方法列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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