-->

can WCF JSON WebService operation with non-string

2019-08-30 00:00发布

问题:

I understood that UriTemplate supports only string parameters unless you have them in form like below (id={id} etc.). Here for example:

Can I pass non-string to WCF RESTful service using UriTemplate?

However I can't make the following work. Even if I change 2nd parameter to string (not string array). Is this kind of operation callable from a browser by typing URL in address-field?

[WebGet(ResponseFormat = WebMessageFormat.Json, 
        UriTemplate = "id={id}/legend={legend}/x={x}/y={y}")]
public Stream GetMapPicture(int id, string[] legend, double x, double y)

All works if I change parameters to strings and type:

http://localhost:8732/Service1/id=732/legend=[343434, 555]/x=43/y=23

Thanks!

回答1:

There is an answer in the link i have in the question:

Can I pass non-string to WCF RESTful service using UriTemplate?

String[]

should probably be string which must be parsed/deserialized.

However with one int and one double it look like this:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Stream GetMapPicture(int id, double x);
…
 public class Service1 : IService1
 {
    [WebGet(ResponseFormat = WebMessageFormat.Json, 
        UriTemplate = "MapPicture/?id={id}&x={x}")]
    public Stream GetMapPicture(int id, double x)
    {

and then it's consumed from browser like this:

http://localhost:8732/service1/MapPicture/?id=3&x=6.21

Because nobody tagged this as duplicate I posted an answer here also. The difference with the link is that here is two parameters combined with ampersand (&)

-m