GET/DELETE背后的原因不能在webapi中有正文

Ped*_*ree 11 c# asp.net-web-api

为什么HttpMethods如GETDELETE不能包含身体

public Task<HttpResponseMessage> GetAsync(Uri requestUri);
public Task<HttpResponseMessage> DeleteAsync(string requestUri);
Run Code Online (Sandbox Code Playgroud)

同样在Fiddler中,如果我提供一个身体,背景会变成红色.但它仍然会在身体上执行.

小提琴图像

所以作为替代方案,我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
Run Code Online (Sandbox Code Playgroud)

我错过了别的什么吗?

Jul*_*obs 7

根据HTTP标准,GET方法旨在检索数据,因此不需要提供请求体.

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

这同样适用于DELETE方法.

  • 由于HTTP 1.1 Delete可以有一个正文,因此并不总是处理.请参阅[此答案](http://stackoverflow.com/a/299696/2151623) (3认同)