【发布时间】:2013-09-08 06:00:07
【问题描述】:
正如标题所说,我在使用 Web API 的 post 方法时出现 500 内部服务器错误。 Get 方法工作正常,只是在 POST 中出错。
我正在使用提琴手发送帖子请求:
响应标头: HTTP/1.1 500 内部服务器错误
请求标头: 用户代理:提琴手 主机:本地主机: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);
}
}
Sooooooo 可能出了什么问题?为什么它不允许我发帖?
【问题讨论】:
-
那么您是否尝试过在调试器打开的情况下运行它,看看您的请求发生了什么?这似乎是找出可能出错的必要的第一步。
-
您的请求正文有错字/您在用户 ID 后缺少引用
"。 -
是的,我在 post 方法上使用了断点,但它们没有被命中。
标签: web-services rest asp.net-mvc-4 asp.net-web-api