【问题标题】:Is there a way to get an ASMX Web Service created in VS 2005 to receive and return JSON?有没有办法让在 VS 2005 中创建的 ASMX Web 服务接收和返回 JSON?
【发布时间】:2010-04-27 17:52:31
【问题描述】:

我正在使用 .NET 2.0 和 Visual Studio 2005 来尝试创建一个可以作为 SOAP/XML 和 JSON 使用的 Web 服务。我阅读了Dave Ward's Answer 到问题How to return JSON from a 2.0 asmx web service (除了阅读Encosia.com 上的其他文章),但我无法弄清楚我需要如何设置我的asmx 文件的代码才能使用JSON 使用jQuery.

两个问题:

  • 如何在我的 .NET 2.0 ASMX 文件中启用 JSON?
  • 什么是可以使用 JSON 使用服务的简单 jQuery 调用?

另外,我注意到由于我使用的是 .NET 2.0,我无法实现 using System.Web.Script.Services.ScriptService


这是演示 ASMX 服务的 C# 代码:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;


/// <summary>
/// Summary description for StockQuote
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class StockQuote : System.Web.Services.WebService {

    public StockQuote () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 

    }

    [WebMethod]
    public decimal GetStockQuote(string ticker)
    {
        //perform database lookup here
        return 8;
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }        
}

这是我在网上找到并尝试修改的jQuery的sn-p:

    $(document).ready(function(){
        $("#btnSubmit").click(function(event){
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "http://bmccorm-xp/WebServices/HelloWorld.asmx",
                data: "",
                dataType: "json"
            })
            event.preventDefault();
        });
    });

【问题讨论】:

    标签: c# .net asp.net jquery web-services


    【解决方案1】:

    您可以将ScriptService 与.NET 2.0 / Visual Studio 2005 一起使用。您只需安装在.NET 3.5 之前发布的ASP.NET AJAX Extensions,然后它就会成为框架的核心组件。

    【讨论】:

    • @Aaronaught 我在 Dave Ward 的回答中看到了这一点(在我的 Q 中引用)并安装了它,但是如何将其添加到我的项目中?例如,如果我转到“添加引用”并查看 .NET 组件,我看不到 System.Web.Script。我需要在较旧的 AJAX 扩展中包含的内容是否有其他名称?
    • +1 再次教育我。自 vs08 测试版以来,我没有使用过 vs05。
    • 我想我明白了一些事情......DLL 是 System.Web.Extensions。您还必须添加项目“Ajax Enabled Website”或类似的东西。我可能会用我发现的内容写一个额外的答案。不过,感谢您的帮助。
    • @Ben:是的,没错。我没有意识到这是对程序集名称的混淆。我删除了之前的评论,不正确,它仍然被安装到 GAC 中。
    【解决方案2】:

    让它与 Visual Studio 2005 和 .NET 2.0 一起工作可能有点棘手,尤其是因为 Internet 上的很多信息都参考了 .NET 3.5,它默认包含 AJAX 组件。正如Aaonaught 提到的,您首先需要安装ASP.NET AJAX Extensions for .NET 2.0

    安装 AJAX 扩展后,您需要添加一个新的“启用 AJAX”网站:转到文件 > 新建 > 网站。选择“启用 ASP.NET AJAX 的网站”。这将具有与 .NET 2.0 中的常规 ASP.NET 站点不同的配置文件,因此选择这种站点很重要。

    接下来,如果您的网络配置中尚未引用它,您需要右键单击您的项目并转到“添加引用”。添加对 System.Web.Extensions 版本 1.0.61025.0 的引用。这是新脚本库所在的位置(更新:我可以确认,如果您在 VS 2005 中将项目设置为“启用 AJAX 的网站”,它将自动在 Web.Config 中包含对该程序集的引用文件)。

    最后两个步骤将允许您在代码中添加对System.Web.Script.Services.ScriptService 的引用。现在您可以将 .asmx Web 服务添加到您的项目中,您需要做的就是在您的服务类之前添加以下属性:[System.Web.Script.Services.ScriptService]。您的代码应该类似于:

    using System;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    
    
    /// <summary>
    /// Summary description for StockQuote
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class StockQuote : System.Web.Services.WebService {
    
        public StockQuote () {
    
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
    
        }
    
        [WebMethod]
        public decimal GetStockQuote(string ticker)
        {
            //perform database lookup here
            return 8;
        }
    
        [WebMethod]
        public string HelloWorld() {
            return "Hello World";
        }
    
    }
    

    您的示例代码的部分问题是您没有调用您在示例中定义的同一个 Web 服务。你已经从 jQuery 调用了HelloWorld.asmx,但你应该调用了StockQuote.asmx/HelloWorld。现在,当您使用 application/json 作为内容类型调用 jQuery 方法时,Web 服务将服从并使用 JSON 而不是 XML 响应。


    JSON POST

    POST http://bmccorm-xp/WebServicesAjax/StockQuote.asmx/HelloWorld HTTP/1.1
    x-requested-with: XMLHttpRequest
    Accept-Language: en-us
    Referer: http://bmccorm-xp/WebServicesAjax/TestJSON.html
    Accept: application/json, text/javascript, */*
    Content-Type: application/json; charset=utf-8
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
    Host: bmccorm-xp
    Content-Length: 0
    Connection: Keep-Alive
    Pragma: no-cache
    

    JSON 响应

    HTTP/1.1 200 OK
    Server: Microsoft-IIS/5.1
    Date: Tue, 27 Apr 2010 19:11:40 GMT
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Cache-Control: private, max-age=0
    Content-Type: application/json; charset=utf-8
    Content-Length: 13
    
    "Hello World"
    

    jQuery POST,请求 XML:

    POST http://bmccorm-xp/WebServicesAjax/StockQuote.asmx/HelloWorld HTTP/1.1
    x-requested-with: XMLHttpRequest
    Accept-Language: en-us
    Referer: http://bmccorm-xp/WebServicesAjax/TestJSON.html
    Accept: application/json, text/javascript, */*
    Content-Type: text/xml; charset=utf-8
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
    Host: bmccorm-xp
    Content-Length: 0
    Connection: Keep-Alive
    Pragma: no-cache
    

    XML 响应

    HTTP/1.1 200 OK
    Server: Microsoft-IIS/5.1
    Date: Tue, 27 Apr 2010 18:54:55 GMT
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Cache-Control: private, max-age=0
    Content-Type: text/xml; charset=utf-8
    Content-Length: 96
    
    <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://tempuri.org/">Hello World</string>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      相关资源
      最近更新 更多