I am thinking about rewriting a WebAPI code in C# ASP.NET Core into F# Giraffe. However, for some particular constructs I can't really find an equivalence, in particular for something like below:
[HttpPost("DocumentValidationCallbackMessage")]
[Consumes("application/x-www-form-urlencoded")]
public async Task<IActionResult> DocumentValidationCallbackMessage([FromForm] string xml)
{
// Controller Action implementation
}
AFAIK, the routing in Giraffe is not powered by controllers but by the function choose:
let webApp =
choose [
GET >=>
choose [
route "/" >=> indexHandler
]
setStatusCode 404 >=> text "Not Found" ]
I can't really figure out how to work around in the F# Giraffe the consequences of the C# ASP.NET Core attributes [Consumes("application/x-www-form-urlencoded")] and [FromForm]: how to retrieve directly the value transferred in a url encoded form.
Any idea?
Choose is just one of many functions exposed in the library to help build your web application. There are many others which we can use to get the same behavior as your sample. Below is a commented code example that illustrates the design goals of Giraffe, which is to allow you to piece together units of functionality into a self-descriptive pipeline:
module Sample =
open Giraffe
/// define a type for the model binding to work against.
/// This is the same as saying 'the incoming form will have an string property called xml'
[<CLIMutable>]
type Model =
{ xml: string }
let documentationValidationCallbackMessage: HttpHandler =
route "DocumentValidationCallbackMessage" // routing
>=> POST // http method
>=> bindForm<Model> None (fun { xml = xml } -> // requires the content type to be 'application/x-www-form-urlencoded' and binds the content to the Model type
// now do something with the xml
setStatusCode 200 // in our case we're just going to set a 200 status code
>=> text xml // and write the xml to the response stream
)
These can all be gone over in more detail at the documentation, which is full of examples.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With