WCF Usage Summary: (1) WCF receives HTTP POST data processing method

Source: Internet
Author: User
Tags http post

In WCF 4.0, there is better support for creating restful APIs for us. By defining Uritemplate,webinvoke, you can quickly develop API interfaces.

Here I record how to receive the post data when the HTTP POST data.

1, method One: stream InputStream input stream method (take a look at the method

For example, my Code

" POST ", Responseformat = Webmessageformat.json, bodystyle ="json/ui/{projectname}/{ entityname}?q={querystring}&id={indentity}")] Updateorinsertentityresponse Updateorinsertentityresponse (String projectName, String entityname, String querystring,string indentity,stream Pstream);

UriTemplate defines a parameter matching relationship.

"Json/ui/{projectname}/{entityname}?q={querystring}&id={indentity}"

The corresponding parameters

String ProjectName, String entityname, String querystring,string indentity

The names must be the same, otherwise they cannot be matched. All fields must be of type string.

How to get Post data information.

Defines the stream pstream parameter.

If you run the application now, you will get an error message on the page:

is Stream. 

How to resolve.

The first step is to modify your own service.svc file.

The original

<%@ ServiceHost language= "C #" debug= "true" service= "Vine.DataMan.RestfulService.EntityService"

Codebehind= "EntityService.svc.cs"%>

Add a new property:

factory= "System.ServiceModel.Activation.WebServiceHostFactory"

The final result is:

<%@ ServiceHost language= "C #" debug= "true" service= "Vine.DataMan.RestfulService.EntityService"

Codebehind= "EntityService.svc.cs" factory= "System.ServiceModel.Activation.WebServiceHostFactory"%>

Modify the configuration file Web. config

<system.serviceModel>

<services>

<service name= "Vine.DataMan.RestfulService.EntityService" >

<endpoint binding= "WebHttpBinding"

contract= "Vine.DataMan.RestfulService.ServiceContracts.IEntityCommonService"

behaviorconfiguration= "webhttp"/>

</service>

</services>

<behaviors>

<endpointBehaviors>

<behavior name= "webhttp" >

<webHttp/>

</behavior>

</endpointBehaviors>

<serviceBehaviors>

<behavior>

<!--to avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above BEFO Re Deployment--

<servicemetadata httpgetenabled= "true"/>

<!--to receive exception details on faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information-

<servicedebug includeexceptiondetailinfaults= "true"/>

</behavior>

</serviceBehaviors>

</behaviors>

<servicehostingenvironment aspnetcompatibilityenabled= "true"/>

</system.serviceModel>

Note the bold text. The behavior of the webhttp must be defined.

WCF Usage Summary: (1) WCF receives HTTP POST data processing method

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.