Web API POST方法返回HTTP/1.1 500内部服务器错误

Obv*_*ous 6 rest web-services asp.net-mvc-4 asp.net-web-api

正如标题所说,当使用Web API的post方法时,我有500个内部服务器错误.Get方法工作正常,只是在POST中出错.

我正在使用fidler发送帖子请求:

响应标头: HTTP/1.1 500内部服务器错误

请求标头: 用户代理:提琴手主机:localhost:45379内容类型:application/jsonContent-Length:41内容长度:41

请求 正文: {"iduser"="123456789","username"="orange"}

这是我的post方法代码:

     // POST api/User
     public HttpResponseMessage Postuser(user user)
     {
        if (ModelState.IsValid)
        {
            db.users.Add(user);
            db.SaveChanges();

            HttpResponseMessage response =R  equest.CreateResponse(HttpStatusCode.Created, user);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.iduser }));
            return response;
       }
       else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sooooooo可能出错了什么?为什么它不允许我发帖?

Fra*_*cis 6

帖子中的数据不是有效的JSON对象,这是模型绑定器所期望的(Content-Type:application/json).

{"iduser"="123456789","username"="orange"}
Run Code Online (Sandbox Code Playgroud)

尝试将你的=替换​​为:并看看你是如何继续的.您的代码在我的机器上运行,并在请求中进行了这些更改.

POST http://localhost:20377/api/test/Postuser HTTP/1.1
Host: localhost:20377
Connection: keep-alive
Content-Length: 42
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Origin: chrome-extension://fhjcajmcbmldlhcimfajhfbgofnpcjmb
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,nb;q=0.4,de;q=0.2

{"iduser":"123456789","username":"orange"}
Run Code Online (Sandbox Code Playgroud)