F#MvcWebApi - Postman - FromBody总是为空

时间:2015-03-18 14:59:50

标签: asp.net-mvc f# postman

我刚刚开始玩F#MvcWebApi。我已经开始使用基本模板,并设法让这个工作达到我将数据放入邮递员请求体内的程度。每当我尝试访问从正文传递的任何值时,由于某种原因它们总是为空,即(在下面的情况下数据将始终为null)任何人都可以帮助我

ValueController代码

type ValuesController() =
    inherit ApiController()

   //Always returs null
   member x.Post ([<FromBody>] data:string) = data

   //Works
   member x.Post ([<FromBody>] data:string) = "Succesful"

我正在使用邮递员来打电话给api enter image description here

enter image description here

1 个答案:

答案 0 :(得分:4)

虽然我不知道 Postman ,但您似乎正在将Content-Type设置为&#34; application / json&#34;在这两种情况下。当你说你发布&#34; application / json&#34; (或&#34; application / xml&#34;),Web API将尝试结构化数据绑定到&#39;模型&#39;。

您是否尝试使用F#记录?

[<CLIMutable>]
type DataModel = { data : string }

type ValuesController() =
    inherit ApiController()

    member x.Post (dataModel : DataModel) = x.Ok data

您还需要add this statement to your configuration

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver <-
    Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()