当前位置:网站首页>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“
边栏推荐
- CLP information - how does the digital transformation of credit business change from star to finger?
- Infiltration learning diary day19
- Software product download collection
- Force buckle day32
- Technical practice online fault analysis and solutions (Part 1)
- Small program graduation project based on wechat reservation small program graduation project opening report reference
- How can enterprises optimize the best cost of cloud computing?
- Applet graduation project based on wechat selection voting applet graduation project opening report function reference
- String hash, find the string hash value after deleting any character, double hash
- 技術實踐|線上故障分析及解决方法(上)
猜你喜欢
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
How to use AHAS to ensure the stability of Web services?
After listening to the system clear message notification, Jerry informed the device side to delete the message [article]
51 MCU external interrupt
Huawei BFD and NQA
What are the advantages and disadvantages of data center agents?
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
Small program graduation project based on wechat reservation small program graduation project opening report reference
Small program graduation design is based on wechat order takeout small program graduation design opening report function reference
AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
随机推荐
Day05 branch and loop (II)
The contact data on Jerry's management device supports reading and updating operations [articles]
File contains vulnerability summary
在寻求人类智能AI的过程中,Meta将赌注押向了自监督学习
Conditional test, if, case conditional test statements of shell script
C import Xls data method summary IV (upload file de duplication and database data De duplication)
C import Xls data method summary V (complete code)
How programmers find girlfriends through blind dates
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Huawei BFD and NQA
Small program graduation project based on wechat e-book small program graduation project opening report function reference
I don't know why it can't run in the project and how to change it
Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
Jerry's modification setting status [chapter]
Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
LeetCode 168. Detailed explanation of Excel list name
How to delete MySQL components using xshell7?
be based on. NETCORE development blog project starblog - (14) realize theme switching function
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators