GET / DELETED背后的原因不能在web api中有主体

时间:2014-09-11 09:15:18

标签: c# asp.net-web-api

为什么HttpMethodGETDELETE不能包含 正文

public Task<HttpResponseMessage> GetAsync(Uri requestUri);
public Task<HttpResponseMessage> DeleteAsync(string requestUri);

也在Fiddler中,如果我提供一个身体,背景会变红。但它仍会在身上执行。

Fiddle Image

作为替代方案,我使用SendAsync(),因为它接受HttpRequestMessage,其中包含HttpMethod以及内容

// other codes
Category category = new Category(){ Description = "something" };
string categoryContent = JsonConvert.SerializeObject(category);
string type = "application/json";

HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Delete, "-page-")
HttpContent content = new StringContent(categoryContent, Encoding.UTF8, type);
HttpClient client = new HttpClient();

message.Content = content;
await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
// other codes

我错过了别的什么吗?

1 个答案:

答案 0 :(得分:7)

根据HTTP标准,GET方法旨在检索数据,因此无需提供请求正文。

添加请求正文违反了定义的规则。因此禁止这样做。

这同样适用于DELETE方法。