Custom QueryStringConverter to support arrays. HttpBehavior / WebHttpBehavior

Topics: Web Api
Aug 30, 2011 at 6:05 PM

I'd like my resource uri templates to support multiple values and be serialized into CLR array types.  I remember there was a bug in WCF REST where it wouldn't use your custom QueryStringConverter.

Does WebApi have support for this yet?

http://api.example.com/endpoint/3/items?types=t1,t2,t3

Can this be accomplished in WebApi's current state?

 

[WebGet(UriTemplate = "{someId}/items?types={itemTypes}")]
HttpResponseMessage
<ServiceResponse<ItemSummary>> GetItems(Int32 someId, String[] itemTypes);

 ------------------------------------------------------------------
public class StringArrayConverter : QueryStringConverter
    {
        public override Boolean CanConvert(Type type)
        {
            return (type == typeof(String[])) || base.CanConvert(type);
        }
 
        public override object ConvertStringToValue(String parameter, Type parameterType)
        {
            if (parameterType == typeof(String[]))
            {
                return parameter.Split(new Char[]{ ',' }, StringSplitOptions.RemoveEmptyEntries);
            }
 
            return base.ConvertStringToValue(parameter, parameterType);
        }
    }