I will show you how to do it. This code is for training purposes only. Here I am not talking about design and best practices, so feel free to change whatever you want.
Well, you should follow these steps:
1) Create your own ApiExplorer:
public class MyApiExplorer: ApiExplorer { private readonly string _version; public MyApiExplorer(string version) : base(GlobalConfiguration.Configuration) { _version = version != null ? version.ToUpperInvariant() : "V1"; foreach(var apiDescription in ApiDescriptions) { apiDescription.RelativePath = apiDescription.RelativePath.Replace("{version}", _version); } } public override bool ShouldExploreController(string controllerVariableValue, HttpControllerDescriptor controllerDescriptor, IHttpRoute route) { return controllerDescriptor.ControllerType.FullName.Contains(_version); }
}
a) In the constructor, the _version will be converted to upperCase (only if it will be passed as lowerCase), but if it is zero, it will take V1 by default. Then change the relative path to show a specific version instead of {version}.
b) ShouldExploreController (in short) decide whether a particular controller will be displayed in the documentation. In this case, we will only show the controllers that the full name of its type contains the selected version.
2) Go to the HelpController class and change the Index method as follows:
public ActionResult Index(string version) {
We replace the current ApiExplorer with our own to be returned when calling Configuration.Services.GetApiExplorer ()
Now you can use this ... / help? version = v1 or ... / help? version = v2 or ... / help? version = v3 and you will get specific api controller documentation.
source share