65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET 3.5 LINQ、WCF、JSON 和 AJAX 示例应用程序

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.89/5 (20投票s)

2008 年 9 月 4 日

CPOL

2分钟阅读

viewsIcon

137313

downloadIcon

4559

一个简单的 LINQ、WCF、JSON 和 AJAX 示例。

引言

本文的目的是提供一个可运行的 .NET 新技术示例。该示例包含一个调用 WCF 服务并通过 JSON 在 JavaScript 中使用结果的有效示例。它还将为您提供一个如何在 LINQ 中调用存储过程的示例。该示例使用您可以从 Microsoft 下载的 Northwind 数据库。要运行该示例,您必须更改 web.config 中的连接字符串“NorthwindConnectionString”,使其指向您自己的 Northwind 副本。存储过程 SearchCustomers 的代码包含在解决方案中的文本文件中。

背景

通常,开发人员忙于实际处理应用程序,没有时间学习 Microsoft 提供的所有新工具。我见过一些 LINQ、WCF 和 JSON 的示例,但我希望将每个示例放在一个地方。

用于 JSON 的实体类

以下类是一个将在 JavaScript 中可访问的实体。为了通过 JSON 访问属性,您需要添加适当的属性。

using System;
using System.Runtime.Serialization;
 
namespace WCF_AJAX.Data
{
    [Serializable]
    [DataContract]
    //Take note of the attributes Serializable, DataContract and DataMember
    //These are what make the object easily usable to you via JSON
    public class CustomerItem
    {   

        [DataMember]
        public string CustomerID {get;  set;}

        [DataMember]
        public string ContactName { get; set; }

        public CustomerItem()
        {

        }
    }
}

通过 JSON 访问实体对象

以下标记文件提供了一个使用从 WCF 服务返回的 CustomerItem 列表的示例。为了使用下面的 JavaScript,您需要一个 ScriptManager 和对 ApplicationService(WCF)的引用。

<asp:scriptmanager id="ScriptManager1" runat="server">
    <services>
        <asp:servicereference path="~/Services/ApplicationService.svc">
        </asp:servicereference>
    </services>
</asp:scriptmanager>"

JavaScript

function Search() {
   var textInput =$get("txtSearch");
   ApplicationService.searchCustomers(textInput.value, OnSearchComplete, OnError);
   $get("ddlCustomers").focus();
}

function OnSearchComplete(result) {
   var selectOfCustomers = $get("ddlCustomers");
   var customerItems = result;
   var pos = 0;
   for (var customerNo in customerItems) {
      var oOption = document.createElement("OPTION");
      oOption.text = customerItems[customerNo].ContactName;
      oOption.value = customerItems[customerNo].CustomerID
      selectOfCustomers.add(oOption, pos);
      pos += 1;
   }
}

function OnError(result) {
   alert(result.get_message());
}

使用 LINQ 创建 WCF 服务以获取您的数据

以下类是从客户端调用的 WCF 服务。需要 AspNetCompatibilityRequirements 属性。

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Collections.Generic;

namespace WCF_AJAX.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = 
         AspNetCompatibilityRequirementsMode.Allowed)]
    public class ApplicationService
    {
        // Add [WebGet] attribute to use HTTP GET
        [OperationContract]
        public List<data.customeritem>searchCustomers(string searchText)
        {
            var listOfCustomerItems = new List<data.customeritem>();
            var dataContext = new Data.NorthwindDataContext();
            foreach (var result in dataContext.SearchCustomers(searchText))
            {
                var item = new Data.CustomerItem();
                item.CustomerID = result.CustomerID;
                item.ContactName = result.ContactName;
                listOfCustomerItems.Add(item);
            }
            return listOfCustomerItems;
        }

        // Add more operations here and mark them with [OperationContract]
    }
}

NorthwindDataContext 类是由设计器创建的。创建该类不需要任何编码。为了使用存储过程,您必须从解决方案中选择“添加新项目”并选择 LINQ to SQL 类。VS 2008 将向项目中添加一个 DBML 文件。然后,在设计模式下打开 DBML 文件,并从服务器资源管理器选项卡的“数据连接”中拖动存储过程。

© . All rights reserved.