Restsharp不返回任何数据获取请求和发布请求

时间:2018-08-15 10:12:17

标签: asp.net-mvc asp.net-web-api restsharp

我正在学习如何使用RestSharp进行Web api调用。当我用Postman发出get请求时,它返回200,而在正文中它给我null而不是json;当我用Postman发出发布请求时,它返回204(无内容)。 欢迎所有建议。谢谢。

这是我的带有套装的产品类别,并获取方法:

        public class Product
         {
          public string ProductName { get; set; }
          public string ProductPrice { get; set; }
          public string Manufacturer { get; set; }
          public string Shop { get; set; }
           }

这是我的产品控制器类:

      public class ProductsController : ApiController
       {
          Product[] p = new Product[]
           {
           new  Product { ProductName = "JIM", Manufacturer = "Kofi",  ProductPrice = "22", Shop = "12"}
           };



    // GET: api/Products
    public IEnumerable<Product> GetAll()
    {
        var client = new RestClient("http://localhost:4282/");
        var request = new RestRequest("api/products", Method.GET) { RequestFormat = DataFormat.Json };
        var response = client.Execute<List<Product>>(request);
        return response.Data;

        }

    // POST: api/Products
    public void Post([FromBody] Product p)
      {
        var client = new RestClient("http://localhost:4282/");
        var request = new RestRequest("api/products", Method.POST);
        request.RequestFormat = DataFormat.Json;
        request.AddJsonBody(p);
        client.Execute(request);
          }

0 个答案:

没有答案