发送多个对象与angularjs到的WebAPI [英] send multiple objects to webapi with angularjs
问题描述
我有以下的控制器在我的ASP.NET的WebAPI应用:
I have the following controller in my ASP.NET WebApi Application:
[Route("api/PutItem/")]
[HttpPut]
public IHttpActionResult PutItem(Guid id, Item item)
{
if (!ModelState.IsValid)
}
在我的项目AngularJs服务,我有以下
And I have the following in my "Items" AngularJs service
var doEditItem = function(item){
var deferred = $q.defer();
console.log('item', item);
var config = {
headers: { 'Authorization': "Bearer " + $rootScope.token }
//,params: {id:item.ItemId}
}
$http.put(sitesettings.url() + "api/PutItem/", item, config)
.success(function(data){
deferred.resolve(data);
})....
我最终收到此消息:
I end up receiving this message:
没有HTTP资源发现,请求URI相匹配
的http://本地主机:63289 / API / PutItem / ',MessageDetail:无动作是
控制器上的项目,该请求匹配找到。}
No HTTP resource was found that matches the request URI 'http://localhost:63289/api/PutItem/'.", MessageDetail: "No action was found on the controller 'Items' that matches the request."}
我已经作出更改参数添加的项目是这样的:
I have made changes to the parameters to add the item like this:
jItem = {id:item.ItemId, item:item}
然后我试图传递到JITEM的put方法是这样的:
and then I've tried passing jItem into to the put method like this:
$http.put(sitesettings.url() + "api/PutItem/", jItem, config)
我的应用程序回升的GUID,而不是新的项目。如果我删除的GUID,送项目,我的应用程序获取新项目。
My application picks up the Guid, but not the new Item. If I remove the Guid, and send the item, my Application picks up the new Item.
我想知道我需要改变我的应用程序,这样我可以发送一个GUID和应用也拿起新的项目。
I would like to know how I need to change my application so that I can send a Guid and the application picks up the new Item too.
我用Angular.toJson(项目,FALSE);但是这似乎并没有改变我的ASP.NET应用程序是如何接收信息。
I have used Angular.toJson(item, false); but this doesn't seem to change how my ASP.NET application is receiving the information.
非常感谢
推荐答案
您不能到的WebAPI操作的原始类型一样,一起发送复杂类型,您可以发送多个原始类型(GUID的,诚信的,字符串等)或单个复杂类型(项目
)。如果你想同时发送,周围的工作是使用 JObject
:
You can not send a complex type along with a primitive type like that to an WebAPI action, you can either send multiple primitive types (Guid's, int's, strings etc.) or a single complex type (Item
). If you want to send both, a work around is to use JObject
:
[HttpPut]
public void PutItem(JObject data)
{
Item item = data["item"].ToObject<Item>();
Guid id = data["id"].ToObject<Guid>();
}
发这样的角度:
$http({
url: "/api/PutItem",
method: "PUT",
data: {id: SOMEGUID, item: { ......} }
});
这篇关于发送多个对象与angularjs到的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!