Aw3same Aw3same - 11 months ago 80
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
,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>
[SwaggerResponse(HttpStatusCode.OK, typeof(Tarea))]
[SwaggerResponse(HttpStatusCode.NotFound, typeof(string))]
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download