ASP.NET Web API Help Pages and Versioning -
i create separate page each version of api. example, user go /help?v=1 see version 1.0 routes , /help?v=2 see version 2.0 routes.
using sdammann.webapi.versioning
, added version
property versionedapiexplorer
return routes defined version , added version argument constructor. tried this:
config.services.add(typeof(iapiexplorer), new versionedapiexplorer(config, "1")); config.services.add(typeof(iapiexplorer), new versionedapiexplorer(config, "2"));
but gives me following error:
the service type iapiexplorer not supported. parameter name: servicetype
i added 1 instance of service - config.services.replace(typeof(iapiexplorer), new versionedapiexplorer(globalconfiguration.configuration, "1"));
- configuration work, test controller. tried this:
foreach (var service in configuration.services.getservices(typeof(iapiexplorer))) { if (service.gettype() != typeof(versionedapiexplorer)) continue; var explorer = service versionedapiexplorer; if (explorer.version == v) { apiexplorer = explorer; } }
this gives same error received above. know use this.configuration.services.getapiexplorer()
don't know how use appropriate instance of versionedapiexplorer
. know instantiate appropriate apiexplorer
directly in controller, prefer keep in configuration file if possible.
so have 2 questions:
- how add 2 services of type
versionedapiexplorer
config object? - how retrieve appropriate service in controller?
or there different approach take accomplish same goal?
thank you!
i ended going solution hinted @ in question. feel there's better solution problem, gets job done.
first, added version
property versionedapiexplorer
:
public string version { get; private set; }
then modified initializeapidescriptions
this:
private collection<apidescription> initializeapidescriptions() { collection<apidescription> apidescriptions = new collection<apidescription>(); var controllerselector = configuration.services.gethttpcontrollerselector(); idictionary<string, httpcontrollerdescriptor> allcontrollermappings = controllerselector.getcontrollermapping(); idictionary<string, httpcontrollerdescriptor> controllermappings = new dictionary<string, httpcontrollerdescriptor>(); // mappings defined version if (allcontrollermappings != null && version != null) { foreach (var key in allcontrollermappings.keys) { if (key.substring(0, key.indexof('.')) == versionedcontrollerselector.versionprefix + version) { controllermappings.add(key, allcontrollermappings[key]); } } } else if (version == null) { controllermappings = allcontrollermappings; } if (controllermappings != null) { foreach (var route in configuration.routes) exploreroutecontrollers(controllermappings, route, apidescriptions); } return apidescriptions; }
i added method use set version:
public void setversion(string version) { this.version = version; this.apidescription = new lazy<collection<apidescription>>(initializeapidescriptions); }
finally, modified helpcontroller
looks this:
public actionresult index(string v) { return this.view(getapiexplorer(v).apidescriptions); } private iapiexplorer getapiexplorer(string version) { if (version == null) { version = "1"; } var apiexplorer = this.configuration.services.getapiexplorer() versionedapiexplorer; if (apiexplorer != null) { apiexplorer.setversion(version); } return apiexplorer; }
Comments
Post a Comment