0

我有下面的简单 api

public class ProductRepository : IProductPrpository
{

    #region IProductPrpository Members

    public IEnumerable<Product> GetAll(string ProductOption)
    {
        return Utility.GetDiscountItems(ProductOption);
    }

    public Product GetProduct(string Id)
    {
        return Utility.GetProduct(Id);
    }
    public String PostBag(Bag bagofItem)
    {
       return  Utility.PostBagDiscountedItem(bagofItem);
    }

    #endregion
}

当我调用 GetProduct 和 GetProduct 时它工作正常,但是当通过不允许的方法发布 PostBag 时 -

http://localhost:54460/api/products?PostBag="

错误请帮忙

有我的客户端脚本将数据发布到 PostBag Api

@model List<MultiBuy.Models.Product>

 @{
  ViewBag.Title = "Index";
 }

 <h2>Items in the bag</h2>
  <table>
    <tr>
    <th> Item_number_option&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </th>
    <th> Option_number &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </th>
    <th> Price &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>    
    <th> PublicationCode &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>    
    <th> Quantity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </th>   

  </tr>

   @foreach (var item in Model)
    {
       <tr>
         <td> @item.ItemNumber</td>
         <td> @item.Option</td>
         <td> @item.Price</td>
         <td> @item.PublicationCode</td>
         <td> @item.Quantity</td>

      </tr>
   }   
 </table>
 <div>
 <ul id="products" />
  <input type="button" value="Search" onclick="find();"  />
  </div>
 @section scripts {
 <script>
  function find() {
  var dataJSON = '@Model';

        $.ajax({
            type: 'POST',
            url: 'http://locallhost:54460/api/products?PostBag=',

            data: JSON.stringify(dataJSON),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json'
        }).done(function (data) {
            $('#products').text(data)
        });
    };

</script>
}

感谢您的所有帮助

4

1 回答 1

-1

您不能以与获取相同的方式发布帖子,获取您可以将参数放入您的网址中,但发布您不能。

http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

于 2013-07-26T09:35:44.317 回答