【问题标题】:ASMX web-services and HTTP GETASMX 网络服务和 HTTP GET
【发布时间】:2010-06-25 13:48:48
【问题描述】:

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

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>

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

【问题讨论】:

    标签: c# .net web-services asmx http-get


    【解决方案1】:

    我想你想要

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

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

    【讨论】:

    • 根据我的个人经验,[ScriptMethod(UseHttpGet = true)] 根本不会影响服务方法。它不会强制该方法仅使用 GET 并且没有它,只要您在 web.config 中允许它,您仍然可以使用 GET 调用该方法
    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多