当前位置:网站首页>Yyds dry goods inventory it's not easy to say I love you | use the minimum web API to upload files
Yyds dry goods inventory it's not easy to say I love you | use the minimum web API to upload files
2022-07-04 01:41:00 【My space IO】
Preface
stay .NET 6 Before , The file upload function is very simple :
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
// Yes file Perform the operation
return Ok(file.FileName);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
however , When using .NET 6 Minimum WEB API To achieve the same function , But unexpectedly encountered many pits .
Implementation code
Convert to minimum WEB API To realize , The code looks like this :
app.MapPost("/upload", async (IFormFile file) =>
{
return Results.Ok(file.FileName);
});
- 1.
- 2.
- 3.
- 4.
however , When called with the same client code , But back to HTTP 415 error :

allow Content Type
This is because when uploading files , The use of Content-Type Not by default application/json, It is multipart/form-data

stay dotnet/aspnetcore Of issues in , We found a solution (https://github.com/dotnet/aspnetcore/issues/35082):

Modify the code as follows :
app.MapPost("/upload", async (IFormFile file) =>
{
return Results.Ok(file.FileName);
}).Accepts<IFormFile>("multipart/form-data");
- 1.
- 2.
- 3.
- 4.
result , Or return to 415 error . What's more strange is that , There is an extra stack of error messages :

Surprise BUG
therefore , Want to find the problem by looking at the code .
Use error messages “Expected a supported JSON media type but got”, We located the source code file Http/Http.Extensions/src/RequestDelegateFactory.cs:
however , Find out master Branches and v6.0.0 Branch code , Huge differences . such as ,master The branches are IFormFile Related codes , and v6.0.0 It's not at all :

View the submission history of the file , We found such a submission :

According to submission , We found the corresponding issue(https://github.com/dotnet/aspnetcore/issues/34303):
#yyds Dry inventory #
It seems , If you want to support IFormFile Parameters , We have to wait until 2022 year 11 month (.NET 7 Expected release date )!!!

Solution
fortunately , The issue It also provides flexible solutions , Pass in HttpRequest Parameters :
app.MapPost("/upload",
async (HttpRequest request) =>
{
var form = await request.ReadFormAsync();
return Results.Ok(form.Files.First().FileName);
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
Verify success .
Conclusion
Back , I've also tried in .NET 6 Use the previous Controller How to achieve , No problem at all .
It seems to be the smallest WEB API Are different implementation mechanisms , I'm not sure if there are any other pits .
Suggest : Minimum WEB API Don't use it for production projects for the time being .
If you think this article has inspired you , Please pay attention to my official account ”My IO“

边栏推荐
- mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- Force buckle day32
- About uintptr_ T and IntPtr_ T type
- Small program graduation project based on wechat e-book small program graduation project opening report function reference
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
- Infiltration learning diary day19
- Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
- Huawei cloud micro certification Huawei cloud computing service practice has been stable
- AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
猜你喜欢

中电资讯-信贷业务数字化转型如何从星空到指尖?

Applet graduation project based on wechat selection voting applet graduation project opening report function reference

MySQL - use of aggregate functions and group by groups

CLP information - how does the digital transformation of credit business change from star to finger?

Huawei cloud micro certification Huawei cloud computing service practice has been stable

SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution

Small program graduation project based on wechat reservation small program graduation project opening report reference

MySQL deadly serial question 2 -- are you familiar with MySQL index?

Douban scoring applet Part-3

TP5 automatic registration hook mechanism hook extension, with a complete case
随机推荐
Special copy UML notes
“疫”起坚守 保障数据中台服务“不打烊”
All in one 1412: binary classification
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
7.1 learning content
A malware detection method for checking PLC system using satisfiability modulus theoretical model
Avoid playing with super high conversion rate in material minefields
Who moved my code!
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
Small program graduation project based on wechat reservation small program graduation project opening report reference
How to view the computing power of GPU?
Trading software programming
Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
2022 R2 mobile pressure vessel filling certificate examination and R2 mobile pressure vessel filling simulation examination questions
Jerry's update contact [article]
Summary of common tools and technical points of PMP examination
be based on. NETCORE development blog project starblog - (14) realize theme switching function
The contact data on Jerry's management device supports reading and updating operations [articles]
Why can't it run (unresolved)