接受 List<CustomObject> 的 ASP.NET Web 方法因“Web 服务方法名称无效"而失败. [英] ASP.NET Web Method that accepts a List&lt;CustomObject&gt; is failing with &quot;Web Service method name is not valid.&quot;

查看:54
本文介绍了接受 List<CustomObject> 的 ASP.NET Web 方法因“Web 服务方法名称无效"而失败.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个接受自定义对象列表(通过 jQuery/JSON 传入)的 Web 方法.

I want to create a web method that accepts a List of custom objects (passed in via jQuery/JSON).

当我在本地运行网站时,一切似乎都正常.jQuery 和 ASP.NET,每个人都很高兴.但是当我把它放在我们的一台服务器上时,它就炸了.jQuery 在 ajax 请求后收到 500 错误,响应为:

When I run the website locally everything seems to work. jQuery and ASP.NET and everyone is happy. But when I put it on one of our servers it blows up. jQuery gets a 500 error after the ajax request with the response being:

System.InvalidOperationException: EditCustomObjects Web 服务方法名称无效.

System.InvalidOperationException: EditCustomObjects Web Service method name is not valid.

这是网络服务方法:

[WebMethod]
public void EditCustomObjects(int ID, List<CustomObject> CustomObjectList)
{
  // Code here
}

还有我的 jQuery 代码(我认为这并不重要,因为错误似乎发生在 Web 服务级别):

And my jQuery code (which I don't think matters, since the error seems to be happening on the web service level):

var data = JSON.stringify({
  ID: id,
  CustomObjectList: customObjectList
});

$.ajax({
  type: "POST",
  url: "/manageobjects.asmx/EditCustomObjects",
  data: data,
  contentType: "application/json; charset=utf-8",
  async: false,
  dataType: "json",
  success: function(xml, ajaxStatus) {
    // stuff here
  }
});

customObjectList 初始化如下:

The customObjectList is initialized like so:

var customObjectList = [];

我像这样向它添加项目(通过循环):

And I add items to it like so (via a loop):

var itemObject = { 
  ObjectTitle = objectTitle,
  ObjectDescription = objectDescription,
  ObjectValue = objectValue
}

customObjectList.push(itemObject);

那么,我在这里做错了什么吗?有没有更好的方法将数据数组从 jQuery 传递到 ASP.NET Web 服务方法?有没有办法解决Web 服务方法名称无效"的问题.错误?

So, am I doing anything wrong here? Is there a better way of passing an array of data from jQuery to an ASP.NET web service method? Is there a way to resolve the "Web Service method name is not valid." error?

仅供参考,我在 Windows Server 2003 机器上运行 .NET 2.0,我从该站点获得了上述代码:http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

FYI, I am running .NET 2.0 on a Windows Server 2003 machine, and I got the code for the above from this site: http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

有人要求提供有关 Web 服务的更多信息,我宁愿不提供整个课程,但这里还有一些可能会有所帮助的信息:

Someone requested some more info on the web service, I'd rather not provide the whole class but here is a bit more that may help:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService] 
public class ManageObjects : Custom.Web.UI.Services.Service 
{
}

巴拉

推荐答案

我根据评论做出假设,您可以直接在浏览器中访问 Web 服务.

I make the assuption based on comments that you can directly go to the web service in the browser.

只是为了将您的自定义对象与配置隔离,您可以放置​​另一个服务,例如:

Just to isolate your custom object from configuration, you could put another service in place like:

[WebMethod]
public static string GetServerTimeString()
{
    return "Current Server Time: " + DateTime.Now.ToString();
}

从客户端 jQuery ajax 调用中调用.如果这有效,那么它可能与您的对象具体相关,而不是服务器端的配置.否则,请继续查看服务器端配置跟踪.

Call that from a client side jQuery ajax call. If this works, then it is probably related to your object specifically and not configuration on the server side. Otherwise, keep looking on the server side config track.

一些示例代码:

[WebMethod(EnableSession = true)]
public Category[] GetCategoryList()
{
    return GetCategories();
}
private Category[] GetCategories()
{
     List<Category> category = new List<Category>();
     CategoryCollection matchingCategories = CategoryList.GetCategoryList();
     foreach (Category CategoryRow in matchingCategories)
    {
         category.Add(new Category(CategoryRow.CategoryId, CategoryRow.CategoryName));
    }
    return category.ToArray();
}

这是我发布复杂数据类型 JSON 值的示例

And here is an example of where I post a complex data type JSON value

[WebMethod]
 public static string SaveProcedureList(NewProcedureData procedureSaveData)
 {
          ...do stuff here with my object
 }

这实际上包括其中的两个对象数组...我的 NewProcedureData 类型是在一个类中定义的,该类对这些对象进行了布局.

This actually includes two arrays of objects inside it... my NewProcedureData type is defined in a class which lays those out.

编辑 2:

以下是我在一个实例中处理复杂对象的方式:

Here is how I handle a complex object in one instance:

function cptRow(cptCode, cptCodeText, rowIndex)
{
    this.cptCode = cptCode;
    this.cptCodeText = cptCodeText;
    this.modifierList = new Array();
//...more stuff here you get the idea
}
/* set up the save object */
function procedureSet()
{
    this.provider = $('select#providerSelect option:selected').val(); // currentPageDoctor;
    this.patientIdtdb = currentPatientIdtdb;// a javascript object (string)
//...more object build stuff.
    this.cptRows = Array();
    for (i = 0; i < currentRowCount; i++)
    {
        if ($('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val() != watermarkText)
        {
            this.cptRows[i] = new cptRow($('.cptIcdLinkRow').eq(i).find('.cptCode').val(), $('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val(), i);//this is a javscript function that handles the array object
        };
    };
};
//here is and example where I wrap up the object
    function SaveCurrentProcedures()
    {

        var currentSet = new procedureSet();
        var procedureData = ""; 
        var testData = { procedureSaveData: currentSet };
        procedureData = JSON.stringify(testData);

        SaveProceduresData(procedureData);
    };
    function SaveProceduresData(procedureSaveData)
    {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: procedureSaveData,
the rest of the ajax call...
        });
    };

注意!重要的是,procedureSaveData 名称必须在客户端和服务器端完全匹配才能正常工作.更多代码示例:

NOTE !IMPORTANT the procedureSaveData name must match exactly on the client and server side for this to work properly. more code example:

using System;
using System.Collections.Generic;
using System.Web;

namespace MyNamespace.NewProcedure.BL
{
    /// <summary>
    /// lists of objects, names must match the JavaScript names
    /// </summary>
    public class NewProcedureData
    {
        private string _patientId = "";
        private string _patientIdTdb = "";

        private List<CptRows> _cptRows = new List<CptRows>();

        public NewProcedureData()
        {
        }

        public string PatientIdTdb
        {
            get { return _patientIdTdb; }
            set { _patientIdTdb = value; }
        }
       public string PatientId
        {
            get { return _patientId; }
            set { _patientId = value; }
        }
        public List<CptRows> CptRows = new List<CptRows>();

}

--------
using System;
using System.Collections.Generic;
using System.Web;

namespace MyNamespace.NewProcedure.BL
{
    /// <summary>
    /// lists of objects, names must match the JavaScript names
    /// </summary>
    public class CptRows
    {
        private string _cptCode = "";
        private string _cptCodeText = "";

        public CptRows()
        {
        }

        public string CptCode
        {
            get { return _cptCode; }
            set { _cptCode = value; }
        }

        public string CptCodeText
        {
            get { return _cptCodeText; }
            set { _cptCodeText = value; }
        }
     }
}

希望这会有所帮助.

这篇关于接受 List<CustomObject> 的 ASP.NET Web 方法因“Web 服务方法名称无效"而失败.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆