当前位置:网站首页>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“

边栏推荐
- Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
- Is Shengang securities company as safe as other securities companies
- Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
- QML add gradient animation during state transition
- Summary of JWT related knowledge
- MySQL uses the view to report an error, explain/show can not be issued; lacking privileges for underlying table
- Since the "epidemic", we have adhered to the "no closing" of data middle office services
- SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
- G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
- C import Xls data method summary III (processing data in datatable)
猜你喜欢
![Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading](/img/d2/99932660298b4a4cddd7e5e69faca1.png)
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading

Use classname to modify style properties

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

SQL statement

Introduction to superresolution

Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page
![Jerry's modification setting status [chapter]](/img/23/d6eb521943b35e543a9681a98ad3be.jpg)
Jerry's modification setting status [chapter]

I don't care about you. OKR or KPI, PPT is easy for you

Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference

长文综述:大脑中的熵、自由能、对称性和动力学
随机推荐
Remember a lazy query error
Should enterprises start building progressive web applications?
MySQL introduction - functions (various function statistics, exercises, details, tables)
Future source code view -juc series
LeetCode 168. Detailed explanation of Excel list name
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
Mobile phone battery - current market situation and future development trend
AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
After listening to the system clear message notification, Jerry informed the device side to delete the message [article]
Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
A little understanding of GSLB (global server load balance) technology
Cancer biopsy instruments and kits - market status and future development trends
Openbionics robot project introduction | bciduino community finishing
Chinese Mitten Crab - current market situation and future development trend
Description of setting items of Jerry [chapter]
Fundamentals of machine learning: feature selection with lasso
HackTheBox-baby breaking grad
Create template profile
Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
be based on. NETCORE development blog project starblog - (14) realize theme switching function