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

Web services Web API POST方法返回HTTP/1.1500内部服务器错误,web-services,rest,asp.net-mvc-4,asp.net-web-api,Web Services,Rest,Asp.net Mvc 4,Asp.net Web Api,正如标题所说,在使用Web API的post方法时,我遇到了500个内部服务器错误。Get方法工作正常,只是在POST中出错 我正在使用fidler发送post请求: 响应标题: HTTP/1.1500内部服务器错误 请求头: 用户代理:Fiddler 主机:本地主机:45379 内容类型:应用程序/json内容长度:41 内容长度:41 请求正文: {“iduser”=“123456789”,“username”=“orange”} 这是我的post方法代码: // POST api

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

我正在使用fidler发送post请求:

响应标题: HTTP/1.1500内部服务器错误

请求头: 用户代理:Fiddler 主机:本地主机:45379 内容类型:应用程序/json内容长度: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);
        }
    }

soooooo可能出了什么问题?为什么不允许我发布?

您发布的内容中的数据不是有效的JSON对象,这正是model binder所期望的(内容类型:application/JSON)

试着用:替换你的=,看看你进展如何。你的代码在我的机器上运行,并在请求中进行了更改

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"}

那么,您是否已尝试在启用调试器的情况下运行它,以查看您的请求发生了什么变化?这似乎是解决问题的必要的第一步。您的请求正文有一个输入错误/在用户id之后缺少一个引号
。是的,我在post方法上使用了断点,但它们没有被击中。在我的情况下,我在两个不同的控制器中使用了相同的URL,但它们有不同的请求正文。
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"}