ind*_*rad 101 c# asp.net-web-api
我有一个自定义复杂类型,我想使用Web API.
public class Widget
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的Web API控制器方法.我想像这样发布这个对象:
public class TestController : ApiController
{
// POST /api/test
public HttpResponseMessage<Widget> Post(Widget widget)
{
widget.ID = 1; // hardcoded for now. TODO: Save to db and return newly created ID
var response = new HttpResponseMessage<Widget>(widget, HttpStatusCode.Created);
response.Headers.Location = new Uri(Request.RequestUri, "/api/test/" + widget.ID.ToString());
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用System.Net.HttpClient来调用该方法.但是,我不确定要传递给PostAsync方法的对象类型以及如何构造它.这是一些示例客户端代码.
var client = new HttpClient();
HttpContent content = new StringContent("???"); // how do I construct the Widget to post?
client.PostAsync("http://localhost:44268/api/test", content).ContinueWith(
(postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
Run Code Online (Sandbox Code Playgroud)
如何以Web API理解的方式创建HttpContent对象?
Jos*_*all 131
该通用HttpRequestMessage<T>
已被删除.这个 :
new HttpRequestMessage<Widget>(widget)
Run Code Online (Sandbox Code Playgroud)
将不再有效.
相反,从这篇文章中,ASP.NET团队已经包含了一些新的调用来支持这个功能:
HttpClient.PostAsJsonAsync<T>(T value) sends “application/json”
HttpClient.PostAsXmlAsync<T>(T value) sends “application/xml”
Run Code Online (Sandbox Code Playgroud)
所以,新代码(来自dunston)变成:
Widget widget = new Widget()
widget.Name = "test"
widget.Price = 1;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:44268");
client.PostAsJsonAsync("api/test", widget)
.ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode() );
Run Code Online (Sandbox Code Playgroud)
dun*_*ton 99
您应该使用该SendAsync
方法,这是一种通用方法,它将服务的输入序列化
Widget widget = new Widget()
widget.Name = "test"
widget.Price = 1;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:44268/api/test");
client.SendAsync(new HttpRequestMessage<Widget>(widget))
.ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode() );
Run Code Online (Sandbox Code Playgroud)
如果您不想创建具体类,可以使用FormUrlEncodedContent
该类创建
var client = new HttpClient();
// This is the postdata
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("Name", "test"));
postData.Add(new KeyValuePair<string, string>("Price ", "100"));
HttpContent content = new FormUrlEncodedContent(postData);
client.PostAsync("http://localhost:44268/api/test", content).ContinueWith(
(postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
Run Code Online (Sandbox Code Playgroud)
注意:你需要将你的id设置为可以为null的int(int?
)
Fab*_*ano 73
请注意,如果您使用的是可移植类库,则HttpClient将不具有PostAsJsonAsync方法.要使用可移植类库将内容发布为JSON,您必须执行以下操作:
HttpClient client = new HttpClient();
HttpContent contentPost = new StringContent(argsAsJson, Encoding.UTF8,
"application/json");
await client.PostAsync(new Uri(wsUrl), contentPost).ContinueWith(
(postTask) => postTask.Result.EnsureSuccessStatusCode());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
200465 次 |
最近记录: |