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

◇◆丶佛笑我妖孽 提交于 2021-01-27 12:15:04

问题


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.


回答1:


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




回答2:


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".



来源:https://stackoverflow.com/questions/55452461/a-list-of-guids-is-empty-when-passed-into-a-model-which-is-used-by-fromform-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!