0

我正在尝试向我的 Web API 发布一些内容,但无论我做什么,模型值总是设置为 null。

我尝试了以下请求(从提琴手复制的跟踪):

POST http://localhost:53176/api/Profile HTTP/1.1
Host: localhost:53176
Connection: keep-alive
Content-Length: 40
Accept: application/xml, text/xml, */*; q=0.01
Origin: http://localhost:53176
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:53176/SignUp
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=do5ddqwghl3v0bhlgr3pyicb

{"InputEmail":"aa","UserName":"asd"}

更新 以及以下内容:

POST http://localhost:53176/api/Profile HTTP/1.1
Host: localhost:53176
Connection: keep-alive
Content-Length: 44
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:53176
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/json
Referer: http://localhost:53176/SignUp
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=do5ddqwghl3v0bhlgr3pyicb

{"InputEmail":"asdas","UserName":"dasd"}

我的 api 看起来像:

    //here I have also tried [FromBody]
    public MyDto Post(MyDto input)
    {
       return input;
    }

MyDto 有两个属性,分别是:

public class MyDto 
{
    string InputEmail { get; set; }
    string UserName { get; set; }
}

有人能告诉我这段代码有什么问题吗,或者我是否遗漏了什么?

我还想知道如何(或在哪里)将调试器放在 webapi 代码中的某个位置,以在 webapi 尝试反序列化之前查看实际请求是什么。

4

1 回答 1

1

您的Content-Type标头是application/x-www-form-urlencoded,但您的正文有 json 数据。将您的内容类型标头修改为application/json或者text/json

编辑:让你的财产Public

于 2013-06-29T00:36:27.077 回答