Aw3same Aw3same - 2 months ago 17
C# Question

Swagger - List of strings in C#

I have a problem with Swagger and C#. I have a GET metod that receives a List of strings, and i prove it with Swagger. When I run the application, in Swagger, the param "Lista" is

paramType = body
instead of
query
,and the debugger receives null in this field. How can I do it? Thanks

Swagger output:

Parameter Value Description Parameter Type Data Type
fecha (required) Fecha query date-time
lista Lista body array[string]


C# Code:

/// <summary>
/// Recover some data
/// </summary>
/// <param name="fecha">Fecha</param>
/// <param name="lista">Lista</param>
/// <returns>Information</returns>
[Route("v1/tareas")]
[SwaggerTags("Planificador")]
[SwaggerResponse(HttpStatusCode.OK, typeof(Tarea))]
[SwaggerResponse(HttpStatusCode.NotFound, typeof(string))]
//[BearerAuthorizeAttribute]
[HttpGet]
public HttpResponseMessage GetTarea(DateTime fecha, List<string> lista = null )
{...}

Answer Source

You have to add attribute [FromBody]:

public HttpResponseMessage GetTarea(DateTime fecha, [FromBody] List<string> lista)

And there is no need to use the default value, it's a reference type if you don't pass anything it will be null anyway.