2011-12-13 72 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