I have a controller action, that accepts an object from the query parameters:
/// <summary>
Get a paginated list with filters.
</summary>
HttpGet]
Route("")]
public virtual async Task<ActionResult<PagedList<ViewType>>> GetMultiple([FromQuery] SearchParametersType parameters)
{
var entities = await _repository.GetMultiple(parameters);
SetPaginationHeaders(entities);
return Ok(_mapper.Map<IEnumerable<ViewType>>(entities).ShapeData(parameters.Fields));
}
In my example the SearchParametersType
looks like this:
public class MovieGenreRelationSearchParameters : SearchParameters
{
[TypeConverter(typeof(ListOfGuidConverter))]
public List<Guid>? MovieIds { get; set; } = new List<Guid>();
[TypeConverter(typeof(ListOfGuidConverter))]
public List<Guid>? GenreIds { get; set; } = new List<Guid>();
}
The payload from the frontend instead sends a comma separated list like this:
/movies/moviegenrerelation?movieIds=08dc7544-985e-4a84-8001-82cdd24bc05c,08dc7542-c799-4f40-80c2-e04d7a1251ac
So I wrote a TypeConverter that converts a comma separated list of strings from a string into a list of Guids which looks like this:
public class ListOfGuidConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value == null) return new List<Guid>();
var guidsRaw = (value as string).Split(',');
return (from guidRaw in guidsRaw select new Guid(guidRaw)).ToList();
}
}
My problem is, that the type converter isn’t used at all. Somebody may know what I am doing wrong?
Thanks for any help 🙂
The TypeConverter is used.