相关疑难解决方法(0)

如何使用System.Net.HttpClient发布复杂类型?

我有一个自定义复杂类型,我想使用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 …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api

101
推荐指数
3
解决办法
20万
查看次数

标签 统计

asp.net-web-api ×1

c# ×1