Does NancyFX supports ASP.NET MVC like \'Catch All\' route? I need one, that basically match every URL. This is very handy for building up Single Page applications.
Is t
Tested in Nancy version 0.23.2
Get[@"/(.*)"]
did not work for me as a catch-all route. The routes "/", "/foo/bar", and longer routes would not catch. It seems like there's no getting around having to define a Get["/"]
route for the root URL. Nothing else seems to catch it (tried Get["{uri*}"]
). Here's how I ended up defining my routes (keep in mind I'm doing this for an Angular application):
Get["/views/{uri*}"] = _ => { return "A partial view..."; };
Get["/"] =
Get["/{uri*}"] = _ =>
{
var uri = (string)_.uri;// The captured route
// If you're using OWIN, you can also get a reference to the captured route with:
var environment = this.Context.GetOwinEnvironment();// GetOwinEnvironment is in the 'Nancy.Owin' namespace
var requestPath = (string)environment["owin.RequestPath"];
return View["views/defaultLayout.html"];
};
It's important to understand Pattern Scoring. The route patterns are weighted, if two routes match the same url segment, the higher score wins. The catch-all pattern is weighted 0
and even though the /views/{uri*}
route pattern is also a catch-all, it starts with a literal, which is weighted 10000
, so it will win out on all routes that start with /views.
Here's more info on Accessing Owin's Environment Variables. Note that the captured uri
variable and requestPath
will be slightly different. The requestPath
will start with a /
where as the uri
variable will not. Also, if the matched route pattern is Get["/"]
, uri
will be null
and requestPath
will be "/"
.
The Views route will return a partial html file, based on the url path, and all other routes will return the default Layout page that will bootstrap the SPA.