ASP.NET在Web服务中调用webmethods - 内部服务器错误

时间:2011-12-13 13:02:27

标签: asp.net web-services jquery asmx webmethod

我有一个名为AEWService.asmx的Web服务,其代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace editor
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class AEWService : System.Web.Services.WebService
    {
        [WebMethod]
        public static Dictionary<string, string> TestFunc(string elementToBeModified, string selectedValue)
        {
            Dictionary<string, string> newValues = new Dictionary<string, string>();
            newValues.Add("k1", "val1");
            newValues.Add("k2", "val2");
            return newValues;
        }
    }
}

当我用ajax调用webmethod时,我得到一个500错误 - 未知的Web方法TestFunc。 ajax调用如下所示:

var dataString = JSON.stringify({ 
    "elementToBeModified": "someElement", 
    "selectedValue": "someValue"
});
$.ajax({
  url: 'AEWService.asmx/TestFunc',
  type: "POST",
  data: dataString,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (data) {
     // do somethig...
  }
});

我还在标签下面的web.config中添加了以下行(我不确定它们是什么意思,但我希望它们没问题):

 <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

当webmethod在aspx页面上时,代码工作得很好。 asmx出了什么问题?

谢谢!

1 个答案:

答案 0 :(得分:4)

您无法从webservice返回字典对象。 看这篇文章。 http://forums.asp.net/t/1370858.aspx/1

也不要将webmethod标记为静态。点击这里 Why are Static Methods not Usable as Web Service Operations in ASMX Web Services?