通过List< Guid>作为web服务参数

本文关键字:web 服务 参数 作为 List Guid 通过 | 更新日期: 2023-09-27 18:17:09

我有一个web方法,在列表作为参数。jQuery ajax调用可以很好地传递guids。然而,web服务在列表中有正确数量的项,但所有项都是空向导。

这是我的方法。
[WebMethod]
public bool CheckProductsAreAvailable(string userId, List<Guid> lId)
{
    // do something
}

我将服务方法调用为:

$('#<%= btnCheck.ClientID %>').click(function () {
var hdnIds = document.getElementById('<%= hdnIds.ClientID %>');
var ids = hdnIds.value; // this contains comma separated guids  

var lId = new Array();
$.each(ids.split(','), function(){
    if(this == '')
        return;
    lId.push({"Guid": this});
});
var data = {"userId": '<%= UserId %>', "lId": lId};
$.ajax({
    type: 'POST',
    url: GetProductsServiceUrl() + '/CheckProductsAreAvailable',
    data: JSON.stringify(data),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (response) {
        if (response.d)
            $('#pAvailability').html('All items are still available.');
        else
            $('#pAvailability').html('Not All items are currently available.');
    },
    error: function (xhr) { alert(xhr.responseText); }
});
return false;
});

我检查了提琴手。传递的值与预期的一致。这里有一个例子。

{"标识":"xxx - xxxx - 0000","盖子":[{"Guid":"cf93114f - d1c9 e011 bdc3 - 0050568 e16a0"},{"Guid":"d093114f - d1c9 e011 bdc3 - 0050568 - e16a0"}"},{"Guid":"751 d7859 - d1c9 e011 bdc3 - 0050568 e16a0"},{"Guid":"761 d7859 - d1c9 e011 bdc3 - 0050568 e16a0"},{"Guid":"771 d7859 - d1c9 e011 bdc3 - 0050568 e16a0"},{"Guid":"781 d7859 - d1c9 e011 bdc3 - 0050568 e16a0"}]}

调试web方法显示userId的正确值(在本例中为xxxx - xxxx -0000)。lId显示有相同数量的id被传递(在本例中为6),但它们都是空guid(0000000 -0000000 -0000000 -000000000000)。

有人知道为什么会这样吗?谢谢。

通过List< Guid>作为web服务参数

lID应该用Guid的字符串表示数组填充,而不是对象数组。

你应该这样修改你的代码:

lId.push(this);

自动反序列化器可以将字符串转换为Guid,因为您必须从字符串初始化Guid(Guid x = new Guid(str);)。将参数更改为字符串列表,并在字符串列表上运行foreach以将它们转换为Guids。

List<Guid> guids = new List<Guid>();    
foreach(string item in lID)
{
    guids.Add(new Guid(item));
}