Postman MVC WebApi Help Documentation
Allows developers expose their WebAPI endpoints so they can be imported into postman as a collection. It generates json that conforms to the Postman Collection Format v1.0.0.
It is based on the original answer to a question on Stack Overflow How to generate JSON Postman Collections from a WebApi2 project using WebApi HelpPages that are suitable for import by Robert H. Engelhardt (rheone) and is released under a Creative Commons Attribution-ShareAlike 3.0 Unported licence.
##Getting Started
-
Install the Nuget package
Install-Package Postman.WebApi.HelpDocumentation
-
Import Api into Postman
Collections -> Import -> Import from a Url -> Success!
Sometime one prefers to use a "human friendly" name for the requests. To do so simply edit the PostManController constructor.
public PostmanApiController()
{
_requestNamingStyle = RequestNamingStyle.Url;
}
There are two options: Url and ActionName (split on Uppercase and drops "Async" keyword.)
-
Project Properties -> Build -> Tick "XML documentation file"
-
Change location to App_Data/XmlDocument.xml
-
Project\Areas\HelpPage\App_Start\HelpPageConfig.cs
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
-
Provide xml comments on controller and actions
If using HttpRequestMessage or HttpResponseMessage the documentation won't provide meaningful examples in either the Help Page or the Postman Help
To cater for this abstration you can configure the WebApi Help with the actual types
Project\Areas\HelpPage\App_Start\HelpPageConfig.cs
config.SetActualRequestType(typeof(MyType), "MyController", "MyAction");
Project\Areas\HelpPage\App_Start\HelpPageConfig.cs
config.SetActualResponseType(typeof(MyType), "MyController", "MyAction");
or
Use the ResponseType attribute on the action
[ResponseType(typeof(MyType))]
public HttpResponseMessage Post(HttpRequestMessage request)
{
}
Sometimes the default values for types are not good enough so it is useful to post meaningful sample data.
Project\Areas\HelpPage\App_Start\HelpPageConfig.cs
config.SetSampleObjects(new Dictionary<Type, object>
{
{
typeof(Person), new Person
{
Id = 1,
Firstname = "Abbott",
Surname = "Robertson",
}
}
});
- Corrected licensing issue
- Made sure schema conformed to Postman json schema file
- Allow a customisation of the request naming style. Url or Action Name.
- Tweaked namespaces that shouldn't have been there
- Json format updated to be work in new Postman packaged app
NuGet.exe pack deploy\Postman.WebApi.HelpDocumentation.nuspec -OutputDirectory dist\