1

我正在尝试创建一个可以执行 HTTP GET 请求的 ASMX Web 服务。我有以下简单的代码片段来说明我已经完成的工作。

using System.Web.Script.Services;
...

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(HttpContext context)
{
 return context.Request.Params.Get("userId").ToString();
}

除此之外,我还在我的 Web.config 文件中添加了以下节点

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

我面临的问题是,System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor每当我尝试调试此 Web 服务时,我都会不断收到可怕的“”错误消息。我不知道问题出在哪里,我非常感谢为使我摆脱这种困境而提供的任何帮助。我意识到 HTTP GET 请求应该非常简单,但我真的不确定我的挫败感是什么。

4

1 回答 1

4

我想你想要

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(int userId)
{
    return userId.ToString();
}

您可以在函数签名中指定参数,Context如果需要,可以将 HttpContext 作为(基类 WebService 上的属性)访问。

于 2010-06-25T16:15:13.317 回答