当前位置:网站首页>. net core minimal API upload file
. net core minimal API upload file
2022-04-22 19:10:00 【Hello, starry sky】
.net 6 Released a new feature Minimal APIs, similar NancyFx frame .
Use Minimal APIs , I feel like I've known you before , Follow asp It's written in a similar way , Don't worry about the messy frame .
for instance : Three lines of code can implement a simple back-end api:
var app = WebApplication.Create(args);
app.MapGet("/",()=>"Hello World!");
app.Run();
Minimalist thinking , make a great bit , hope Entity Framework There are similar simplified versions , similar Java In the technology stack MyBatis, A lot of freedom , Easy to get started , Easy to understand .
Now learn how to use Minimal api Write a file to upload webapi:
Back end code :
var app = WebApplication.Create(args);
app.MapGet("/", () => "Hello .net mini api");
app.MapPost("/upload", async Task<IResult> (HttpRequest request) =>
{
if (!request.HasFormContentType)
{
return Results.BadRequest();
}
var form = await request.ReadFormAsync();
var formFile = form.Files["file"];
if (formFile is null || formFile.Length == 0)
return Results.BadRequest();
await using var stream = formFile.OpenReadStream();
var reader = new StreamReader(stream);
var text = await reader.ReadToEndAsync();
return Results.Ok(text);
});
app.Run();
Here comes Upload text , Read and return function .
Use Fiddler test :

版权声明
本文为[Hello, starry sky]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221904263053.html
边栏推荐
猜你喜欢

MySQL query with serial number

关于字符串常量池,intern方法的理解

Cvpr2022 𞓜 collaborative dual stream visual language pre training model for cross modal retrieval

Type of Flink window

Small LED screen / digital alarm clock display screen / LED billboard / temperature digital display and other LED nixie tube display driver ic-vk1640 / 1640B sop28 / ssop24 package

LeetCode 41. 缺失的第一个正数

leetcode:642. Design search automatic completion system

Error c4996 'fopen': this function or variable may be unsafe Consider using fopen_ s instead. To disabl

Can deleted photos be restored? 3 tips to restore deleted photos

项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(小地图修改完善)
随机推荐
项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(小地图工作前期知识:摄像机)
被删除的相片能恢复吗?3个技巧恢复被删除的相片
k9. Chapter 2: install kubernetes v1.0 based on binary package 20 -- basic environment configuration and kernel configuration (I)
What does %[^\n] mean in C?
青训营-刷题打卡-控制并发执行goroutine的数量
数据库索引
什么是 SAML 断言?
剪切的文件在哪里?文件剪切丢失如何恢复?仅需3步
Small LED screen / digital alarm clock display screen / LED billboard / temperature digital display and other LED nixie tube display driver ic-vk1640 / 1640B sop28 / ssop24 package
微服务负载均衡器Ribbon介绍
C#与 Halcon 联合编程
MCU infrared module knowledge sharing theory is the basis of practical combat in the future
错误 C4996 ‘fopen‘: This function or variable may be unsafe. Consider using fopen_s instead. To disabl
指纹识别记录
Project training - Design and development of 2D multiplayer scuffle game based on unity (small map modified and improved)
欢迎页展示
GB8624-2012 与GB8624-2006 有什么区别?
[best practice] patrol item: turn on URL authentication in content distribution network (CDN)
Data time-sharing market, data backup and individual stock rise and fall development of today's index items [11]
Will map () in JS change the original array