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

标签 c# forms asp.net-core f# f#-giraffe

我正在考虑将 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 中的路由不是由 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 的设计目标,即允许您将功能单元拼凑成一个 self 描述的管道:

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/

相关文章:

jquery - 使用 Jquery 重置表单

Javascript 表单验证仅适用于 firefox

javascript - 在 React js 上下载文件作为字节数组传递

c# - `[FromQuery]` IEnumerable<SomeObject> 在 ASP.NET Core 3.1 中解析?

c# - 如何编写具有智能感知支持的 string.Format 之类的方法

C# 套接字和 SslStreams

C# 互操作 : excel process not exiting after adding new worksheet to existing file

css - 表单内处于错误状态的语义 UI 输入元素

c# - 如何使用正则表达式从字符串中获取值

asp.net-core - 如何在 MVC6 中忽略路由