Restful Get方式请求:
Restful服务 Get请求方式:http://localhost:10718/Service1.svc/Get/A/B/C
http://localhost:10718/Service1.svc 服务地址;Get 方法名;A,B,C分别为三个String参数的值。
请求所得数据将在页面显示
代码实现:
简单示例:一个查询方法,获取三个参数,并将得到的参数显示到页面
1.接口契约
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Serialization; 5 using System.ServiceModel; 6 using System.ServiceModel.Web; 7 using System.Text; 8 9 namespace WcfService110 {11 [ServiceContract]12 public interface IService113 {14 [OperationContract]15 [WebGet(UriTemplate = "Get/{StrA}/{StrB}/{StrC}",16 BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]17 string GetData(string StrA,string StrB,string StrC);18 }19 20 }
2.接口服务
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.Serialization; 5 using System.ServiceModel; 6 using System.ServiceModel.Web; 7 using System.Text; 8 9 namespace WcfService110 {11 [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]12 public class Service1 : IService113 {14 public string GetData(string StrA, string StrB, string StrC)15 {16 return string.Format("You entered: A:{0},B:{1},C:{2}", StrA, StrB, StrC);17 }18 }19 }
3. 配置文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <system.web> 4 <compilation debug="true" targetFramework="4.0" /> 5 </system.web> 6 <system.serviceModel> 7 <services> 8 <service behaviorConfiguration="GetPostBehavior" name="WcfService1.Service1"> 9 <endpoint address="" behaviorConfiguration="GetPostEndBehaviors" binding="webHttpBinding"10 contract="WcfService1.IService1">11 <identity>12 <dns value="localhost" />13 </identity>14 </endpoint>15 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />16
Restful Post方式请求:
WCF Restful 服务 Get/Post请求