【发布时间】:2014-12-23 17:27:01
【问题描述】:
我有一个像这样执行 POST 调用的 C# 客户端:
public void Add(ModelA newObj)
{
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.x.com/api/ModelA");
string json = JsonConvert.SerializeObject(newObj, Formatting.None,
new JsonSerializerSettings {
PreserveReferencesHandling = PreserveReferencesHandling.All
});
var messagebyte = Encoding.ASCII.GetBytes(json);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
httpRequest.ContentLength = messagebyte.Length;
// Send request
using (var stream = httpRequest.GetRequestStream())
{
stream.Write(messagebyte, 0, messagebyte.Length);
stream.Close();
}
// Get response
using (var response = httpRequest.GetResponse() as HttpWebResponse)
{
response.Close();
}
}
控制器看起来像这样:
[ResponseType(typeof(ModelA))]
public IHttpActionResult PostModelA(ModelA newModelA)
{
return NotFound();
}
令人惊讶的是,客户端的响应是 200 OK。此外,“response.Method”的属性是“GET”而不是“POST”。我在这里错过了什么?
【问题讨论】:
-
显然,我的 POST 调用被路由到 GET。我已将“return NotFound()”放在 GET 处理程序下,但出现“未找到”错误。这个问题变得非常奇怪
标签: rest c#-4.0 asp.net-web-api2