Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

httpclient

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"));

and am looking for something like

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.

like image 297
Sarah Avatar asked Dec 03 '14 09:12

Sarah


1 Answers

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; }
    }
}

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
            }
        ]
    }
]

Going Forward

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

More details on these in VS Mag

like image 161
Rhumborl Avatar answered Sep 28 '22 18:09

Rhumborl