In addition to static string paths, we can specify route arguments.
Suave comes with a cool feature called "typed routes", which gives you statically typed control over arguments for your route. As an example, let's see how we can add
id of an album to the details route:
This might look familiar to print formatting from C++, but it's more powerful.
What happens here is that the compiler checks the type for the
%d argument and complains if you pass it a value which is not an integer.
The WebPart will apply for requests like
In the above example, there are a few important aspects:
sprintf "Details: %d" idis statically typed string formatting function, expecting the id as an integer
(fun id -> OK ...)is an anonymous function or lambda expression if you like, of type
int -> WebPart
- the lambda expression is passed as the second parameter to
- first argument of
pathScanfunction also works as a statically typed format
- type inference mechanism built into F# glues everything together, so that you do not have to mark any type signatures
To clear things up, here is another example of how one could use typed routes in Suave for request
14: 15: 16:
We will not however keep that route in our application.
For more information on working with strings in a statically typed way, visit this site
Full name: SuaveMusicStore.webPart
Full name: Suave.WebPart.choose
Full name: Suave.Filters.path
Full name: Suave.Successful.OK
Full name: Suave.Filters.pathScan
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Full name: Suave.Web.startWebServer
Full name: Suave.Web.defaultConfig
Show code from this section on GitHub