ASP.NET调用一个web服务内部的webMethods - 内部服务器错误 [英] ASP.NET Calling webmethods inside an webservice - Internal Server Error

查看:253
本文介绍了ASP.NET调用一个web服务内部的webMethods - 内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为AEWService.asmx具有以下code的Web服务:

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

当我打电话与阿贾克斯的WebMethod,我得到一个500错误 ​​- 未知的Web方法TestFunc。 Ajax调用看起来是这样的:

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

我还添加以下行到下标签的web.config(我不知道他们是什么意思,但我希望他们都OK了):

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"/>

在code的工作完美的,当WebMethod的是一个aspx页面上。什么是错误的ASMX?

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

谢谢!

推荐答案

您不能从web服务返回一个字典对象。
看到这个帖子。 http://forums.asp.net/t/1370858.aspx/1

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

另外不要标记的WebMethod为静态。这里检查
<一href=\"http://stackoverflow.com/questions/1263379/why-are-static-methods-not-usable-as-web-service-operations-in-asmx-web-services\">Why是静态方法不能用作Web服务操作在ASMX Web服务?

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

这篇关于ASP.NET调用一个web服务内部的webMethods - 内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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