我正在考虑将 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
}

AFAIK,Giraffe 中的路由不是由 Controller 提供支持,而是由函数 choose 提供支持:

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

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

任何想法?

最佳答案

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

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 中更详细地讨论,其中包含大量示例。

希望这可以帮助!

关于c# - 如何在 F# Giraffe Web API 中检索 url 编码的表单?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56028830/

10-12 14:54