如何在 F# Giraffe Web API 中检索 url 编码形式?

How to retrieve a url encoded form in F# Giraffe Web API?

我正在考虑将 C# ASP.NET Core 中的 WebAPI 代码重写到 F# Giraffe 中。 但是,对于某些特定的构造,我无法真正找到等效项,特别是对于以下内容:

[HttpPost("DocumentValidationCallbackMessage")]
[Consumes("application/x-www-form-urlencoded")]
public async Task<IActionResult> DocumentValidationCallbackMessage([FromForm] string xml)
{
    // Controller Action implementation
}

据我所知,Giraffe 中的路由不是由控制器驱动,而是由函数 choose:

驱动
let webApp =
    choose [
        GET >=>
            choose [
                route "/" >=> indexHandler
            ]
        setStatusCode 404 >=> text "Not Found" ]

我真的不知道如何在 F# Giraffe 中解决 C# ASP.NET 核心属性 [Consumes("application/x-www-form-urlencoded")][FromForm] 的后果:如何直接检索值以 url 编码形式传输。

有什么想法吗?

Choose 只是库中公开的众多函数之一,可帮助您构建 Web 应用程序。我们可以使用许多其他方法来获得与您的示例相同的行为。下面是一个注释代码示例,说明了 Giraffe 的设计目标,即允许您将功能单元拼凑成一个 self-descriptive 管道:

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
      )

这些都可以在充满示例的 documentation 中更详细地讨论。

希望对您有所帮助!