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

I am creating a small test tool that should provide the user with a list of web services (built using WebAPI). The user should be able to choose a service for testing. I use

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 I'm looking for something like

 client.GetAllWebServices() 

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

+8
source share
1 answer

Michael was right in saying ApiExplorer . This gives you detailed information about all WebApi methods for you. You just need to format it as you want an answer.

Here is a simple example to get a list of all methods with their parameters and return types. Of course, you can make this much more comprehensive - just scan 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; } } } 

It's nice that this is the WebApi call itself, so you can use the HttpClient to call and process it using http://www.localhost.com/api/ApiMethod/Methods . Here is an example 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 } ] } ] 

Forward

Getting comments on XML documents is not so clear, but there is a tutorial on MSDN Blogs .

In addition, there are other packages available that you can use to capture, steal, which are similar to what you need, for example

More on these in VS Mag

+10
source share

All Articles