通过方法不允许将对象发布到web api - http:// localhost:54460 / api / products?PostBag =" [英] posting object to web api though Method Not Allowed - http://localhost:54460/api/products?PostBag="
本文介绍了通过方法不允许将对象发布到web api - http:// localhost:54460 / api / products?PostBag ="的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨那里
i得到了简单的api
Hi there
i have got the bellow simple 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时它通过上面的错误
请帮助
有我的客户端脚本将数据发布到PostBag Api < br $>
it is working fine when i call GetProduct & GetProduct but when post for PostBag it through the above error
please help
there is my client side script which post data to PostBag Api
@model List<MultiBuy.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Items in the bag</h2>
<table>
<tr>
<th> Item_number_option </th>
<th> Option_number </th>
<th> Price </th>
<th> PublicationCode </th>
<th> Quantity </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://localhost:54460/api/products?PostBag=',
data: JSON.stringify(dataJSON),
contentType: 'application/json; charset=utf-8',
dataType: 'json'
}).done(function (data) {
$('#products').text(data)
});
};
</script>
}
感谢您的所有帮助
appreciate all your help
推荐答案
.ajax( {
类型:' POST',
url:' http:// localhost:54460 / api / products?PostBag =',
data:JSON.stringify( dataJSON),
contentType:' application / json; charset = utf-8',
dataType:' json'
})。done(function(data){
.ajax({ type: 'POST', url: 'http://localhost:54460/api/products?PostBag=', data: JSON.stringify(dataJSON), contentType: 'application/json; charset=utf-8', dataType: 'json' }).done(function (data) {
(' #products')。text(data)
});
};
< / script >
}
('#products').text(data) }); }; </script> }
感谢您的所有帮助
appreciate all your help
在wbApiConfig的顶部添加一条路线如下所示
Add one more route on the top in wbApiConfig like below
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Post" }
);
/api/Product/PostBag
希望这有帮助
Hope this helps
这篇关于通过方法不允许将对象发布到web api - http:// localhost:54460 / api / products?PostBag ="的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文