[英]Action method not executed when FromForm model binding list of complex type which contains IFormFile property
I'm now working on a project using AspNetCore 2.2 to build a pure RESTful application. 我现在正在使用AspNetCore 2.2构建纯RESTful应用程序的项目中。 And right now I run into a weird bug when implementing a
multipart/form-data
upload API. 现在,当实现
multipart/form-data
上传API时,我遇到了一个奇怪的错误。
I tried to use my domain object as model on the POST API. 我试图将域对象用作POST API上的模型。 And this is my Controller.
这是我的控制器。
[ApiController]
[Route("api/v{v:apiVersion}/[controller]")]
[ApiVersion("1")]
public class PhotoController : ControllerBase
{
[HttpPost]
public string Upload([FromForm]TestMultipartModel model)
{
return "success";
}
}
...and my Model looks like this. ...我的模特看起来像这样。
public class TestMultipartModel
{
[Required]
public int? Num { get; set; }
public IFormFile File { get; set; }
public string Str { get; set; }
public List<TestMultipartSubModel> Details { get; set; }
}
public class TestMultipartSubModel
{
public string Name { get; set; }
// public IFormFile File { get; set; }
}
I use Postman to test my API as below. 我使用Postman如下测试我的API。 (I'd love to post a screenshot of Postman but my reputation is too low to do that :(
(我很想发布Postman的屏幕截图,但我的声誉太低,无法做到这一点:(
num: "1"
str: "some string..."
file: "random.png"
details[0].name: "my_name"
details[0].file: "another_random.png"
details[1].name: "second_name"
details[1].file: "second_random.png"
Everything works fine as if the sub-model doesn't contains a IFormFile
property. 一切正常,就好像子模型不包含
IFormFile
属性一样。
But if I un-comment that line, after posting API my CPU will start bursting but the program would do nothing but print these log: 但是,如果我取消注释该行,则在发布API之后,我的CPU将开始爆裂,但该程序除了打印以下日志外将不执行任何操作:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:44388/api/v1/photo multipart/form-data; boundary=--------------------------565570716756230895912873 30896
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executing endpoint 'MyProject.Controllers.PhotoController.Upload (MyProject)'
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Route matched with {action = "Upload", controller = "Photo", page = ""}. Executing controller action with signature System.String Upload(MyProject.Controllers.TestMultipartModel) on controller MyProject.Controllers.PhotoController (MyProject).
But it supposed to print this line after these 3 lines, (and of course, run my Upload
method). 但是它应该在这三行之后打印此行(当然,运行我的
Upload
方法)。
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executing action method MyProject.Controllers.PhotoController.Upload (MyProject) - Validation state: Valid
After a few hours of debugging, I still have no idea how to fix this. 经过几个小时的调试,我仍然不知道如何解决此问题。
If anyone can tell me 1) if this is a bug from Microsoft or 2) how can I fix this bug (like implement my own custom IModelBinder or something else), I would very appreciate! 如果有人可以告诉我1)这是Microsoft的错误,还是2)如何解决此错误(例如实现自己的自定义IModelBinder或其他功能),我将不胜感激!
After Googling with the keyword aspnetcore ModelBinding infinite loop
I found my answer in this link . 在使用关键字
aspnetcore ModelBinding infinite loop
进行谷歌搜索之后,我在此链接中找到了答案。
Seems like Microsoft has fixed this bug in Asp.Net Core 3.0, so what I have to do now is convincing my team to upgrade our project... 好像微软已经修复了Asp.Net Core 3.0中的这个错误,所以我现在要做的是说服我的团队升级我们的项目...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.