本文介绍了当传递到ASP.NET Core 2.2中的[FromForm]使用的模型时,GUID的列表为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

骗子骗子.请参阅我自己的修复程序.

我目前在使用HTTP POST和 [FromForm] 将GUID列表传递给控制器​​操作时遇到一些问题.

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

导航列表为EMPTY.如果我尝试将其转换为字符串列表,而不是具有3个值,则列表中只会显示1个值,并且它是包含3个值但用逗号分隔的字符串.

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.

我使用[FromForm]的原因是因为我需要上传文件,但还要将其他一些数据传递给操作.

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

我创建了一个示例项目,当您使用Kestrel调试应用程序时,该项目会使用swagger.

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

bug 端点需要一个GUID列表和一个字符串列表,并输出您发布的内容.当您发布2个Guid和2个字符串时,您应该期望它们都将被打印出来.在这种情况下,找到并打印了0个向导,并打印了1个字符串,其中包含您发布但用逗号分隔的值.
works 端点使用[FromBody]并可以正常工作.

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.

这是项目: https://github.com/sander1095/FromFormGuidListBug

对于不想看项目的人:

控制器:

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

    return Ok(message);
}

型号:

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调用: 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""

结果:

Ids 为空,而 IdsAsStringList 包含1个值(而不是2个),这是将2个字符串传递给1个逗号分隔的值.

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

推荐答案

看来我的问题在于Swagger,而不是ASP.NET Core

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

当我执行以下CURL命令时,东西起作用:

When I execute the following CURL command, stuff works:

curl -X POST"https://localhost:5001/api/Bug/bug" -H接受:文本/纯文本" -H内容类型: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"

这里的区别在于,它代替了使用
-F"Ids =" 9dfb212a-a215-4991-9452-3ddf90e21ec0," 9dfb212a-a215-4991-9452-3ddf90e21ec0"
我用
-F"Ids =" 9dfb212a-a215-4991-9452-3ddf90e21ec0"-F" Ids ="9dfb212a-a215-4991-9452-3ddf90e21ec0""(重复的 Ids 字段.)

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

我在招摇的仓库中创建了一个问题 https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1107

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

这篇关于当传递到ASP.NET Core 2.2中的[FromForm]使用的模型时,GUID的列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:41