Url parameters
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:
App.fs
12: 13: |
|
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 $rootUrl/store/details/28
.
In the above example, there are a few important aspects:
sprintf "Details: %d" id
is 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 typeint -> WebPart
- the lambda expression is passed as the second parameter to
pathScan
function - first argument of
pathScan
function 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 $rootUrl/store/details/abba/1
:
App.fs
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
from Suave
from Suave
from Suave
from Suave
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