当前位置:网站首页>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“
边栏推荐
- Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
- ThinkPHP uses redis to update database tables
- Solution to the problem that jsp language cannot be recognized in idea
- LeetCode 168. Detailed explanation of Excel list name
- 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?
- 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
- Sequence sorting of basic exercises of test questions
- Cancer biopsy instruments and kits - market status and future development trends
- 0 basic learning C language - nixie tube dynamic scanning display
- Three layer switching ②
猜你喜欢
SQL statement
Small program graduation project based on wechat examination small program graduation project opening report function reference
Openbionics exoskeleton project introduction | bciduino community finishing
CLP information - how does the digital transformation of credit business change from star to finger?
Huawei BFD and NQA
In the process of seeking human intelligent AI, meta bet on self supervised learning
How to use AHAS to ensure the stability of Web services?
I don't care about you. OKR or KPI, PPT is easy for you
技术实践|线上故障分析及解决方法(上)
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
随机推荐
Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
Conditional statements of shell programming
Fundamentals of machine learning: feature selection with lasso
Idsia & supsi & usi | continuous control behavior learning and adaptive robot operation based on Reinforcement Learning
MySQL introduction - functions (various function statistics, exercises, details, tables)
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
Hash table, string hash (special KMP)
Feign implements dynamic URL
What are the advantages and disadvantages of data center agents?
How to delete MySQL components using xshell7?
中电资讯-信贷业务数字化转型如何从星空到指尖?
Huawei cloud micro certification Huawei cloud computing service practice has been stable
技术实践|线上故障分析及解决方法(上)
CLP information - how does the digital transformation of credit business change from star to finger?
51 single chip microcomputer timer 2 is used as serial port
AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
Small program graduation project based on wechat reservation small program graduation project opening report reference
Should enterprises start building progressive web applications?
Future source code view -juc series
Hbuilder link Xiaoyao simulator