Is there a way to list all available routes in the Nancy app?

I am implementing the API through a web service using Nancy.

I would like to have a / help or / docs page that programmatically lists all the routes available so that I can provide API users with automatically generated / updated documentation.

Any ideas on how to do this? (Inside the route handler, "this.routes" provides access to a collection of specific routes, but only to the current NancyModule. I need a programmatic way to list all registered routes, not just the current module)

+6
source share
3 answers

You can do this by taking a dependency on IRouteCacheProvider and calling GetCache - we really do this in one of our demos, mostly a repo:

https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Demo.Hosting.Aspnet/MainModule.cs#L13

+8
source

Not exactly what you need, but Nancy also has an integrated dashboard. To enable it, follow these steps:

public class CustomBootstrapper : DefaultNancyBootstrapper { protected override DiagnosticsConfiguration DiagnosticsConfiguration { get { return new DiagnosticsConfiguration { Password = @"secret"}; } } } 

And then you can access it on {yournancyapp} / _ nancy

https://github.com/NancyFx/Nancy/wiki/Diagnostics

+10
source

An example of using IRouteCacheProvider , e.g. @grumpydev mentioned in this answer :

 // within your module public class IndexModule : NancyModule { // add dependency to IRouteCacheProvider public IndexModule(Nancy.Routing.IRouteCacheProvider rc) { routeCache = rc; Get["/"] = GetIndex; } private Nancy.Routing.IRouteCacheProvider routeCache; private dynamic GetIndex(dynamic arg) { var response = new IndexModel(); // get the cached routes var cache = routeCache.GetCache(); response.Routes = cache.Values.SelectMany(t => t.Select(t1 => t1.Item2)); return response; } } public class IndexModel { public IEnumerable<Nancy.Routing.RouteDescription> Routes { get; set; } } 

You can get routing information, such as Path and Method , from the Nancy.Routing.RouteDescription list. For example, with this view:

 <!DOCTYPE html> <html> <body> <p>Available routes:</p> <table> <thead><tr><th>URL</th><th>Method</th></tr></thead> <tbody> @Each.Routes <tr><td>@Current.Path</td><td>@Current.Method</td></tr> @EndEach </tbody> </table> </body> </html> 
0
source

All Articles