web api虽然方法不允许

时间:2013-07-26 09:26:25

标签: asp.net-web-api

我有一个简单的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&amp; GetProduct但是当PostBag通过Method Not Allowed -

发布时
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>
}

感谢您的所有帮助

1 个答案:

答案 0 :(得分:-1)

你不能以与获取相同的方式发帖,获取你可以在你的网址中输入参数,但发布你不能。

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