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 that possible?
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 that possible?
Answer provided by @synhershko does not work for me. It does not handle /users/2 or any other route containing more segements.
Below code works on my machine ;) :
My solution is not perfect, cause it does not match everything. I repeated as many '/(.*)' as in my longest Angular route.
Yes, using Regex
But you don't really need it for building a Single Page Application with NancyFX - you can just use Get and Post with all your routing logic and still have a single page app.
An updated answer for whom @synhershko 's solution does not work. Try:
This will capture all paths except for the index page. I am not sure if this will work in the context of Angular, but this worked for me when trying to hack together a simple server with just one handler.
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 aGet["/"]
route for the root URL. Nothing else seems to catch it (triedGet["{uri*}"]
). Here's how I ended up defining my routes (keep in mind I'm doing this for an Angular application):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 weighted10000
, 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 andrequestPath
will be slightly different. TherequestPath
will start with a/
where as theuri
variable will not. Also, if the matched route pattern isGet["/"]
,uri
will benull
andrequestPath
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.