WCF restful service get/POST requests

Source: Internet
Author: User

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请求

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.