WCF(Windows Communication Foundation) - Web编程模型:使用WCF创建REST服务,使用asp.net ajax调用WCF服务 System.ServiceModel.Activation.WebServiceHostFactory - 用于承载使用 WCF Web 编程模型的服务 System.ServiceModel.Activation.WebScriptServiceHostFactory - 能够向服务中自动添加 ASP.NET AJAX 终结点而无需进行配置


化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)


作者:webabcd



介绍

WCF(Windows Communication Foundation) - Web编程模型:使用WCF创建REST服务,使用asp.net ajax调用WCF服务

    ·System.ServiceModel.Activation.WebServiceHostFactory - 用于承载使用 WCF Web 编程模型的服务

    ·System.ServiceModel.Activation.WebScriptServiceHostFactory  - 能够向服务中自动添加 ASP.NET AJAX 终结点而无需进行配置



示例(使用WCF创建REST服务)

1、服务

User.cs

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Collections.Generic;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Text;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Runtime.Serialization;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxnamespace WCF.ServiceLib.Web

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// User实体类

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    [DataContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    public class User

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 用户名

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [DataMember(Order = 0)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        public string Name 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{ get; set; }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 生日

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [DataMember(Order = 1)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        public DateTime DayOfbirth 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{ get; set; }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41}

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

IREST.cs

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Collections.Generic;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Text;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel.Web;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxnamespace WCF.ServiceLib.Web

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// 演示REST(Representational State Transfer)的接口

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// <remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// HTTP方法中:

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// PUT相当于Update

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// GET相当于Read

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// POST相当于Create

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// DELETE相当于Delete

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    /// </remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    [ServiceContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    public interface IREST

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 创建用户

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <param name="name">用户名</param>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <param name="dayOfbirth">生日</param>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// WebInvoke - 指示服务操作在逻辑上就是调用操作,而且可由 Web 编程模型调用

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// UriTemplate - 用于服务操作的统一资源标识符 (URI) 模板。URI模板可以将一个 URI 或一组 URI 映射到服务操作。有关 URI 模板的更多信息,请参见 UriTemplate 和 UriTemplateTable

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// Method - 与操作关联的协议方法,默认为 POST

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// ResponseFormat - 指定从服务操作发出的响应的格式。Xml 或 Json

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// </remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [OperationContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [WebInvoke(

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            UriTemplate = "User/{name}/{dayOfbirth}",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            Method = "PUT",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            ResponseFormat = WebMessageFormat.Json)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        User CreateUser(string name, string dayOfbirth);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 获取用户信息

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <param name="name">用户名</param>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// WebGet - 指示服务操作在逻辑上就是检索操作,而且可由 Web 编程模型调用

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// </remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [OperationContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [WebGet(

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            UriTemplate = "User/{name}",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            ResponseFormat = WebMessageFormat.Json)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        User GetUser(string name);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 更新用户信息

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <param name="name">用户名</param>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <param name="dayOfbirth">生日</param>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// <returns></returns>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [OperationContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [WebInvoke(UriTemplate = "User/{name}/{dayOfbirth}",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            Method = "POST",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            ResponseFormat = WebMessageFormat.Json)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        bool UpdateUser(string name, string dayOfbirth);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 删除用户信息

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <param name="name">用户名</param>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// <returns></returns>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [OperationContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [WebInvoke(

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            UriTemplate = "User/{name}",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            Method = "DELETE",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            ResponseFormat = WebMessageFormat.Json)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        bool DeleteUser(string name);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41}

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

REST.cs

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Collections.Generic;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Text;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxnamespace WCF.ServiceLib.Web

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// 演示REST(Representational State Transfer)的类

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    public class REST : IREST

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        public User CreateUser(string name, string dayOfbirth)

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14            return new User 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{ Name = name, DayOfbirth = DateTime.Parse(dayOfbirth) };

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        public User GetUser(string name)

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14            return new User 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{ Name = name, DayOfbirth = new DateTime(1980, 2, 14) };

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        public bool UpdateUser(string name, string dayOfbirth)

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            return true;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        public bool DeleteUser(string name)

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            return true;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41}

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax


2、宿主

REST.svc

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11<%化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Web.REST" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>注:System.ServiceModel.Activation.WebServiceHostFactory - 用于承载使用 WCF Web 编程模型的服务


Web.config

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<?xml version="1.0"?>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<configuration>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    <system.serviceModel>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        <behaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <serviceBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <behavior name="WebBehavior">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <serviceMetadata httpGetEnabled="true" />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                </behavior>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            </serviceBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <endpointBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <behavior name="RESTBehavior">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <!--webHttp - 启用 WCF 服务的 Web 编程模型-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <webHttp />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                </behavior>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            </endpointBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        </behaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        <services>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <!--name - 提供服务的类名-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <!--behaviorConfiguration - 指定相关的服务行为配置-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <service name="WCF.ServiceLib.Web.REST" behaviorConfiguration="WebBehavior">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--address - 服务地址-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--binding - 通信方式-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--contract - 服务契约-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--behaviorConfiguration - 指定相关的端点行为配置-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <endpoint address="" binding="webHttpBinding" contract="WCF.ServiceLib.Web.IREST" behaviorConfiguration="RESTBehavior" />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            </service>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        </services>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    </system.serviceModel>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax</configuration>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax


3、客户端

REST.aspx

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11<%化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="REST.aspx.cs"

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41    Inherits="Web_REST" Title="WCF创建REST" %>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax</asp:Content>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    <asp:Label ID="lblMsg" runat="server" />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax</asp:Content>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

REST.aspx.cs

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Collections;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Configuration;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Data;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Web;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Web.Security;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Web.UI;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Web.UI.HtmlControls;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Web.UI.WebControls;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Web.UI.WebControls.WebParts;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Xml.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Net;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxpublic partial class Web_REST : System.Web.UI.Page

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    protected void Page_Load(object sender, EventArgs e)

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        var client = new WebClient();

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        var create = client.UploadString("http://localhost:3502/ServiceHost/Web/REST.svc/User/webabcd/1980-2-14", "PUT", string.Empty);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        var read = client.DownloadString("http://localhost:3502/ServiceHost/Web/REST.svc/User/webabcd");

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        var update = client.UploadString("http://localhost:3502/ServiceHost/Web/REST.svc/User/webabcd/1980-2-14", "POST", string.Empty);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        var delete = client.UploadString("http://localhost:3502/ServiceHost/Web/REST.svc/User/webabcd", "DELETE", string.Empty);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        lblMsg.Text = string.Format("{0}<br />{1}<br />{2}<br />{3}",

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            create,

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            read,

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            update,

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            delete);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41}

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax


运行结果:

{"Name":"webabcd","DayOfbirth":"\/Date(319305600000+0800)\/"}

{"Name":"webabcd","DayOfbirth":"\/Date(319305600000+0800)\/"}

true

true




示例(使用asp.net ajax调用WCF服务)

1、服务

User.cs

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Collections.Generic;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Text;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Runtime.Serialization;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxnamespace WCF.ServiceLib.Web

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// User实体类

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    [DataContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    public class User

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 用户名

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [DataMember(Order = 0)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        public string Name 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{ get; set; }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 生日

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [DataMember(Order = 1)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        public DateTime DayOfbirth 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{ get; set; }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41}

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

IAJAX.cs

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Collections.Generic;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Text;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxnamespace WCF.ServiceLib.Web

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// 演示AJAX的接口

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    [ServiceContract(Namespace = "WCF")]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    public interface IAJAX

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// 获取用户

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        /// <param name="name">用户名</param>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        /// <returns></returns>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        [OperationContract]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        User GetUser(string name);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41}

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

AJAX.cs

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Collections.Generic;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Linq;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.Text;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxusing System.ServiceModel.Activation;

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajaxnamespace WCF.ServiceLib.Web

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    /**//// <summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// 演示AJAX的类

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// </summary>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// <remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// ASP.NET 兼容性模型:

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// 如果在负载平衡或者甚至 Web 园的环境中承载 WCF 服务,并且在该环境中后续的会话请求可以被此环境内的不同宿主或进程处理,则需要对会话状态进行进程外持久存储。最新的 WCF 不支持会话状态的持久存储。相反,WCF 将它的所有会话状态存储在内存中。如果在 IIS 中承载 WCF 服务,最后可以使用回收方案。

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// WCF 依赖于会话状态的 ASP.NET 实现,而不是为会话全部再次建立持久存储。此方式有一个严重的限制:使服务仅限于 HTTP

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// ASP.NET 会话状态不是受 ASP.NET 兼容性模式支持的唯一功能。它还支持诸如 HttpContext、globalization 和模拟等功能,就像用于 ASP.NET Web 服务 (ASMX) 一样

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    /// </remarks>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    /// [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15    public class AJAX : IAJAX

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14    化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15        public User GetUser(string name)

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14        化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14            return new User 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{ Name = name, DayOfbirth = new DateTime(1980, 2, 14) };

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16        }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16    }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41}

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax


2、宿主

AJAX.svc

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11<%化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Web.AJAX" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>注:System.ServiceModel.Activation.WebScriptServiceHostFactory  - 能够向服务中自动添加 ASP.NET AJAX 终结点而无需进行配置


Web.config

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<?xml version="1.0"?>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<configuration>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    <system.serviceModel>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        <behaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <serviceBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <behavior name="WebBehavior">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <serviceMetadata httpGetEnabled="true" />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                </behavior>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            </serviceBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <endpointBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <behavior name="AJAXBehavior">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <!--enableWebScript - 启用 WCF 服务的 脚本 编程模型-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                    <enableWebScript />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                </behavior>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            </endpointBehaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        </behaviors>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        <services>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <!--name - 提供服务的类名-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <!--behaviorConfiguration - 指定相关的服务行为配置-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <service name="WCF.ServiceLib.Web.AJAX" behaviorConfiguration="WebBehavior">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--address - 服务地址-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--binding - 通信方式-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--contract - 服务契约-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <!--behaviorConfiguration - 指定相关的端点行为配置-->

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <endpoint address="" binding="webHttpBinding" contract="WCF.ServiceLib.Web.IAJAX" behaviorConfiguration="AJAXBehavior" />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            </service>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        </services>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    </system.serviceModel>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax</configuration>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax


3、客户端

Demo.aspx

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11<%化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="Demo" %>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<html xmlns="http://www.w3.org/1999/xhtml">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<head runat="server">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    <title>AJAX调用WCF</title>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax</head>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax<body>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    <form id="form1" runat="server">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax         <asp:ScriptManager ID="ScriptManager1" runat="server">

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            <Services>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax                <asp:ServiceReference Path="Service/AJAX.svc" />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax            </Services>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        </asp:ScriptManager>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax        <asp:Label ID="lblMsg" runat="server" />

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_10化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_html_11        <script type="text/javascript">化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15       

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            function pageLoad()

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14            化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15                var proxy = new WCF.IAJAX();

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15                proxy.GetUser("webabcd", onSuccess);

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16            }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15           

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15            function onSuccess(result)

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_13化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_14            化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_12{

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15                $get('<%= lblMsg.ClientID %>').innerHTML =

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15                    String.format("姓名:{0}<br />生日:{1}", result.Name, result.DayOfbirth.format("yyyy-MM-dd"));

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_xml_16            }

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_15

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_用户名_41        </script>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax    </form>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax</body>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax</html>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)_ajax


运行结果:

姓名:webabcd

生日:1980-02-14



OK