2011-12-13 74 views
1

我有一个名为AEWService.asmx用下面的代码的Web服务:ASP.NET调用Web服务的内部webMethods的 - 内部服务器错误

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; 
     } 
    } 
} 

当我打电话与阿贾克斯的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文件下的标签(我不知道他们是什么意思,但我希望他们都ok):

<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有什么问题?

谢谢!

+0

把一个try/catch的网络方法,看看是否有任何错误正在提出... –

回答

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?

+0

我很高兴,如果函数抛出一个关于返回类型的异常。但问题是我不能调用该函数:)谢谢! – VladN

+0

愚蠢的我...我从aspx复制/粘贴WebMethod到asmx,我忘了静态:)非常感谢! – VladN