当前位置:网站首页>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“
边栏推荐
- Ceramic metal crowns - current market situation and future development trend
- Trading software programming
- Solution to the problem that jsp language cannot be recognized in idea
- About uintptr_ T and IntPtr_ T type
- SQL statement
- It's corrected. There's one missing < /script >, why doesn't the following template come out?
- Make drop-down menu
- MySQL uses the view to report an error, explain/show can not be issued; lacking privileges for underlying table
- Day05 branch and loop (II)
- Pesticide synergist - current market situation and future development trend
猜你喜欢
Lightweight Pyramid Networks for Image Deraining
Should enterprises start building progressive web applications?
Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page
How to use AHAS to ensure the stability of Web services?
Applet graduation project based on wechat selection voting applet graduation project opening report function reference
Some other configurations on Huawei's spanning tree
Huawei rip and BFD linkage
Magical usage of edge browser (highly recommended by program ape and student party)
JVM performance tuning and practical basic theory - medium
Use classname to modify style properties
随机推荐
Use classname to modify style properties
2022 new examination questions for safety management personnel of hazardous chemical business units and certificate examination for safety management personnel of hazardous chemical business units
Prose article appreciation - the rain in the warm country has never changed into cold, hard and brilliant flowers. Knowledgeable people think he is monotonous, and he thinks he is unlucky, doesn't he?
Maximum likelihood method, likelihood function and log likelihood function
In the process of seeking human intelligent AI, meta bet on self supervised learning
MySQL statement learning record
Software product download collection
QML add gradient animation during state transition
Sequence sorting of basic exercises of test questions
Huawei rip and BFD linkage
Gee: create a new feature and set corresponding attributes
IPv6 experiment
Do you know the eight signs of a team becoming agile?
be based on. NETCORE development blog project starblog - (14) realize theme switching function
1189. Maximum number of "balloons"
Huawei cloud micro certification Huawei cloud computing service practice has been stable
About uintptr_ T and IntPtr_ T type
Flutter local database sqflite
Avoid playing with super high conversion rate in material minefields
Logical operator, displacement operator