Michael ha ragione a dire ApiExplorer
. Questo ti fornisce i dettagli di tutti i metodi WebApi per te. Hai solo bisogno di formattarlo come vuoi la risposta.
Ecco un semplice esempio per ottenere un elenco di tutti i metodi con i relativi parametri e tipi di ritorno. Ovviamente si può rendere questo molto più completo - basta sfogliare gli oggetti per trovare ciò che serve:
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; }
}
}
La cosa bella è che si tratta di un WebAPI stessa chiamata, in modo da poter utilizzare il HttpClient
per chiamare ed elaborarlo utilizzando http://www.localhost.com/api/ApiMethod/Methods. Ecco la risposta del campione 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
}
]
}
]
Andando avanti
Ottenere doc XML commenta fuori non è così netta, ma c'è un tutorial su MSDN Blogs.
Inoltre, ci sono altri pacchetti disponibili che è possibile utilizzare, collegare in, rubare, che fanno simile a ciò che è necessario, per esempio
Altro dettagli su questi in VS Mag
Controllare [ApiExplorer] (http://blogs.msdn.com/b/yaohuang1/archive/2012/06/15/using-apiexplorer-to-export-ap i-information-to-postman-a-chrome-extension-for-testing-web-apis.aspx), potrebbe farti andare. – Michael
Questo mi dà queste: - \t \t methodInfos \t {System.Reflection.MethodInfo [43]} \t System.Reflection.MethodInfo [] + \t \t [0] \t {System.Net.Http.Headers.HttpRequestHeaders get_DefaultRequestHeaders ()} \t System.Reflection.MethodInfo + \t \t [1] \t {System.Uri get_BaseAddress()} \t + \t \t [2] \t {set_BaseAddress Void (System.Uri)} \t + \t \t [3] \t {System.TimeSpan get_Timeout()} \t ecc ... Non fornisce i metodi scritti dallo sviluppatore, come "GetAllCustomers()" – Sarah
Sembra che Kobi abbia ragione quando dice "no" sapere "qualcosa sui tuoi specifici URL API". Forse dovrei usare qualcos'altro invece di HTTPClient? – Sarah