【问题标题】:Getting to the query string (GET request array) inside a web service in .NET获取 .NET 中 Web 服务中的查询字符串(GET 请求数组)
【发布时间】:2008-11-18 09:13:35
【问题描述】:

我正在寻找一种方法来访问包含在 Web 服务内的标准 ASP.NET 请求对象中的 .net 查询字符串。换句话说,如果我将 SOAP Web 服务设置为此 url:

http://localhost/service.asmx?id=2

我可以访问 ID Get 变量吗?

【问题讨论】:

    标签: asp.net web-services get


    【解决方案1】:

    我只是在 asmx 文件中寻找上下文的“请求”,我看到了。但我不确定它是否正确。

    this.Context.Request.QueryString["id"];
    

    【讨论】:

    • 网络服务类。在此示例中,它是“服务”;
    【解决方案2】:

    HttpContext.Current.Request.QueryString["id"]

    【讨论】:

      【解决方案3】:

      既然你问,我猜是没有 HttpContext.Current.Request 吧?

      【讨论】:

        【解决方案4】:

        在寻找相同问题的解决方案时,我决定采用不同的方法。 我的查询字符串包含很多变量,因为我无法从 Web 服务访问查询字符串数据,而且我也不想将每个查询字符串变量作为单独的参数发送,所以我准备了我的 Web 方法来期待一个附加字符串参数。

        那个参数是 window.location(页面的整个 url)在我的 .aspx 页面上的 javascript 函数中

        一旦我的网络服务中有 url,剩下的就很简单了

        Uri myRef = new Uri(stringMyWindowLocationParameter);
        System.Collections.Specialized.NameValueCollection mojQuery = HttpUtility.ParseQueryString(myRef.Query);
        

        现在我的查询字符串包含在 myRef 对象中,这就是我的称呼

        // Instead trying to request query string like this
        string myId = HttpContext.Current.Request.QueryString["id"];
        
        // ... I called it like this
        string myId = myRef["id"];
        

        也许这不是最优雅的方式,但它解决了我的问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-01
          • 2014-07-19
          • 1970-01-01
          • 2016-06-25
          • 2017-07-31
          • 2021-03-22
          相关资源
          最近更新 更多