Ped*_*ree 11 c# asp.net-web-api
为什么HttpMethods如GET和DELETE不能包含身体?
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)
我错过了别的什么吗?
根据HTTP标准,GET方法旨在检索数据,因此不需要提供请求体.
添加请求正文违反了定义的规则.因此禁止这样做.
这同样适用于DELETE方法.
| 归档时间: |
|
| 查看次数: |
6213 次 |
| 最近记录: |