ASP.NET Calling webmethods inside an webservice - Internal Server Error

為{幸葍}努か 提交于 2020-01-05 04:42:22

问题


I have an web service named AEWService.asmx with the following code:

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

When I call the webmethod with ajax, I get a 500 error - Unknown web method TestFunc. The ajax call looks like this:

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

I also added the following lines into web.config under tags (I'm not sure what they mean but I hope they are 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"/>

The code worked perfect when the webmethod was on a aspx page. What's wrong with the asmx?

Thanks!


回答1:


You can not return a dictionary object from webservice. See this post. http://forums.asp.net/t/1370858.aspx/1

Also don't mark the webmethod as static. Check here Why are Static Methods not Usable as Web Service Operations in ASMX Web Services?



来源:https://stackoverflow.com/questions/8489707/asp-net-calling-webmethods-inside-an-webservice-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!