I've created the following RESTful WCF service, which works just fine when running it in VS.
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sales/start={start}&end={end}")]
List<Sales> GetSalesByDate(string start, string end);
However, when deploying this to my test server (running Win2K3 and IIS6) I received the following server error:
Operation 'GetSalesByDate' in contract 'ISalesService' uses GET, but also has body parameter 'start'. GET operations cannot have a body. Either make the parameter 'start' a UriTemplate parameter, or switch from WebGetAttribute to WebInvokeAttribute.
Obviously I have already made 'start' a UriParameter. So can anyone tell me why an exception is being thrown?
EDIT: For reference, here is my configuration file:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="Services.SalesService">
<endpoint behaviorConfiguration="webBehavior"
binding="webHttpBinding"
contract="Services.ISalesService"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
UriTemplate and UriTemplateTable form the basis of the URI-based dispatch engine in WCF. These classes can also be used on their own, allowing developers to take advantage of templates and the URI mapping mechanism without implementing a WCF service.
WebInvoke Method Type = POST as we are implementing POST. URI Template defines the URL format by which this method is identified/linked. Note: MethodName, URI Template name & Operation Contract names may not be same means, they can be different. PostSampleMethod will accept XML string as input in POST method.
It turns out /sales/start={start}&end={end}
is not a valid Uri (duh!). After a little trial and error I finally figured this out. Tweaking the UriTemplate with a '?' solved the problem.
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sales/?start={start}&end={end}")]
List<Sales> GetSalesByDate(string start, string end);
Thanks for your help!
I know it is really late but why didn't you use the following format.
UriTemplate = "/sales/{start}/{end}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With