In WCF 4.0, it provides better support for creating restful APIs. By defining uritemplate, webinvoke can quickly develop API interfaces.
Here I will record how to receive post data during HTTP POST.
1. Method 1: stream inputstream input stream method (check the method carefully)
For example, my code
[Operationcontract]
[Webinvoke (method = "Post", responseformat = webmessageformat. JSON, bodystyle = webmessagebodystyle. Bare,
Uritemplate = "JSON/UI/{projectname}/{entityname }? Q = {querystring} & id = {indentity} ")]
Updateorinsertentityresponse
(String projectName, String entityName, String queryString, String indentity, Stream pstream );
UriTemplate defines the parameter matching relationship.
"Json/ui/{projectName}/{entityName }? Q = {queryString} & id = {indentity }"
Corresponding Parameter
String projectName, String entityName, String queryString, String indentity
The name must be the same; otherwise, the name cannot match. All fields must be of the String type.
How to obtain POST data information.
Define the Stream pstream parameter.
If you run the application now, an error message is displayed on the page:
System. invalidoperationexception: For request in operation updateorinsertentityresponse to be a stream the operation
Must have a single parameter whose type is Stream.
How to solve the problem.
Step 1: Modify your own Service. svc file.
Convert the original
<% @ ServiceHost Language = "C #" Debug = "true" Service = "Vine. DataMan. RestfulService. EntityService"
CodeBehind = "EntityService. svc. cs" %>
Add new attributes:
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 before deployment -->
<ServiceMetadata httpGetEnabled = "true"/>
<! -- To receive exception details in 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 webhttp behavior must be defined.