Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A list of GUIDs is empty when passed into a model which is used by [FromForm] in ASP.NET Core 2.2

EDIT:

THE ISSUE LIES WITH SWAGGER. SEE THE ANSWER FOR MY OWN FIX.

I am currently experiencing some issues with passing a list of GUID's to a controller action by using HTTP POST and [FromForm].

The list of guids is EMPTY. If I try to turn it into a list of strings, instead of having 3 values, only 1 value is seen in the list and it is a string that contains the 3 values but comma seperated.

The reason why I use [FromForm] is because I need to upload a file but ALSO pass some other data to the action.

I have created a sample project which uses swagger when you use Kestrel to debug the application.

The bug endpoint expects a list of GUIDs and a list of strings and outputs the things you post. When you post 2 guids and 2 strings, you should expect both of them to be printed. In this case, 0 guids are found and printed and 1 string is printed which contains the values you posted but comma seperated.
The works endpoint uses [FromBody] and works fine.

Here's the project: https://github.com/sander1095/FromFormGuidListBug

For the people that do not want to see the project:

Controller:

[HttpPost("bug")]
public ActionResult<string> Bug([FromForm] BugModel bugModel)
{
    var message = GetMessage(bugModel);

    return Ok(message);
}

Model:

public class BugModel
{
    /// <summary>
    /// If you send a GUID it will not appear in this list
    /// </summary>
    public IEnumerable<Guid> Ids { get; set; }


    /// <summary>
    /// If you send 3 strings, the list will contain 1 entry with the 3 string comma separated.
    /// </summary>
    public IEnumerable<string> IdsAsStringList { get; set; }
}

CURL call: curl -X POST "https://localhost:5001/api/Bug/bug" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0","9dfb212a-a215-4991-9452-3ddf90e21ec0"" -F "IdsAsStringList="9dfb212a-a215-4991-9452-3ddf90e21ec0","9dfb212a-a215-4991-9452-3ddf90e21ec0""

RESULT:

Ids is empty while IdsAsStringList contains 1 value (instead of 2) which is the 2 strings passsed into 1 comma seperated value.

like image 999
S. ten Brinke Avatar asked Oct 20 '25 05:10

S. ten Brinke


2 Answers

It appears my issue lies with Swagger, NOT with ASP.NET Core

When I execute the following CURL command, stuff works:

curl -X POST "https://localhost:5001/api/Bug/bug" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0"" -F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0"" -F "IdsAsStringList="9dfb212a-a215-4991-9452-3ddf90e21ec0","9dfb212a-a215-4991-9452-3ddf90e21ec0""

The difference here is that instead of using
-F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0","9dfb212a-a215-4991-9452-3ddf90e21ec0"",
I use
-F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0"" -F "Ids="9dfb212a-a215-4991-9452-3ddf90e21ec0"" (Duplicate Ids fields.)

I created an issue on the swagger repo https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1107

like image 148
S. ten Brinke Avatar answered Oct 22 '25 03:10

S. ten Brinke


I think that your request is the problem. When posting a list I always add indices to the name of each element in the list. Try changing the name of the Ids to Ids[0]="firstguidhere" Ids=[1]="secondguidhere".

like image 31
Steffen Jørgensen Avatar answered Oct 22 '25 03:10

Steffen Jørgensen