Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Explore ApiControllers from external assemblies

$
0
0

Hi. 

What I want is to develop an application to explore ApiControllers in external assemblies in order to generate documentation (for example, Word or txt document).

I know about Web Api Help Page but it is not what I need.

I have an assembly:

var assembly = Assembly.LoadFrom(name);

I've tried 2 ways.

Way 1: CustomAssemblyResolver and ApiExplorer

public class CustomAssemblyResolver : DefaultAssembliesResolver
{
	private readonly Assembly assembly;

	public CustomAssemblyResolver(Assembly assembly)
	{
		this.assembly = assembly;
	}

	public ICollection<Assembly> GetAssemblies()
	{
		return new[] { this.assembly };
	}
}

private void GenerateAssemblyDescription(Assembly assembly)
{
	var httpConfiguration = new HttpConfiguration();
httpConfiguration.Services.Replace(typeof(IDocumentationProvider), this.context.XmlDocumentationProvider); httpConfiguration.Services.Replace(typeof(IAssembliesResolver), new CustomAssemblyResolver(assembly)); var apiDescriptions = httpConfiguration.Services.GetApiExplorer().ApiDescriptions; // <-- Empty }

But in this case I can't get apiDescriptions.

Way 2: Reflexion and HttpActionDescriptor

private void GenerateAssemblyDescription(Assembly assembly, IEnumerable<string> namespaceNames)
{
	// var httpConfiguration = new HttpConfiguration();
	// httpConfiguration.Services.Replace(typeof(IDocumentationProvider), this.context.XmlDocumentationProvider);
	// httpConfiguration.Services.Replace(typeof(IAssembliesResolver), new CustomAssemblyResolver(assembly));
	// var apiDescriptions = httpConfiguration.Services.GetApiExplorer().ApiDescriptions;

	foreach (var namespaceName in namespaceNames.OrderBy(x => x))
	{
		var types =
			(from t in assembly.GetLoadableTypes()
			 where t.IsClass && t.IsPublic 
			 /* && typeof(ApiController).IsAssignableFrom(t)*/ && t.Namespace == namespaceName
			 select t).OrderBy(x => x.Name).ToList();

		// Generate the description for each type
		types.ForEach(this.GenerateDescription);
	}
}

private void GenerateDescription(Type type)
{
	var httpConfiguration = new HttpConfiguration();

	var apiControllerSelection = new ApiControllerActionSelector();
	var apiDescriptor = new HttpControllerDescriptor(httpConfiguration, type.Name, type);
	ILookup<string, HttpActionDescriptor> apiMappings = apiControllerSelection.GetActionMapping(apiDescriptor);

	foreach (var maps in apiMappings)
	{
		foreach (HttpActionDescriptor httpActionDescriptor in maps)
		{
			this.GenerateDescription(httpActionDescriptor);
		}
	}
}

private void GenerateDescription(HttpActionDescriptor httpActionDescriptor)
{
	// RelativePath ???
	// Route Values ???
	// Request Body ???
}

But how to get RelativePath (/api/controller/action/id?parameters), ad other information that ApiExplorer contains?

Thanks.


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>