Fork me on GitHub

.NET6之MiniAPI(特别篇):.NET7 Preview1&2

  在写.NET6 MiniAPI的时候,.NET7的新功能也在发展,这里就用特别篇把新加的特点分享给大家,本篇先说Preview1和2

  特点1

  文件上传,demo如下:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapPost("/upload", async (IFormFile file) =>
{
    Console.WriteLine(file.FileName);
    using var stream = System.IO.File.OpenWrite(file.FileName);
    await file.CopyToAsync(stream);
});

app.MapPost("/uploads", async (IFormFileCollection files) =>
{
    foreach (var file in files)
    {
        Console.WriteLine(file.FileName);
        using var stream = System.IO.File.OpenWrite(file.FileName);
        await file.CopyToAsync(stream);
    }
});
app.Run();

  用postman测试,设置Content-Type为multipart/form-data,然后在Body中如下配置,在单文件上传时,key的值要与参数的名称相同。

 

 

   多文件上传方式:

  特点2

  同时还要以流的方式接收客户端数据:

using Microsoft.AspNetCore.Server.Kestrel.Core;

var builder = WebApplication.CreateBuilder(args);
//增加上传流数据的大小
builder.Services.Configure<KestrelServerOptions>(options =>
{
    //默认是30M
    options.Limits.MaxRequestBodySize = int.MaxValue;
});

var app = builder.Build();

app.MapPost("/uploadbig", async (Stream body, CancellationToken cancellationToken) =>
{
    if (body.CanRead)
    {
        var setLength = 10240;
        var readLength = 0;
        var step = 1;
        do
        {
            var arr = new byte[setLength];
            readLength = await body.ReadAsync(arr, 0, arr.Length);
            Console.WriteLine($"第{step++}次读取,流的长度:{readLength}");
        } while (readLength >= setLength);
    }
    await Task.Delay(1);
});
app.Run();

  用postman测试:

  特点3

  在Preview2中,增加了同名参数,就是QueryString的参数名字可以相同,后台用数组接收,但数组类型只能是基础类型,或StringValues:

using Microsoft.Extensions.Primitives;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet("/strs1", (StringValues strs) =>
{
    var backStr = new StringBuilder();
    foreach (var str in strs)
    {
        backStr.Append($"str={str},");
    }
    return backStr.ToString().TrimEnd(',');
});

app.MapGet("/strs2", (string[] strs) =>
{
    var backStr = new StringBuilder();
    foreach (var str in strs)
    {
        backStr.Append($"str={str},");
    }
    return backStr.ToString().TrimEnd(','); 
});
app.Run();

  用postman测试:

  特点4

  如果自定义类型怎么办,系统也提供了一个转换的方式,在类型中实现TryParse方法,达到类型转换的效果:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet("/points", (Point[] points) =>
{
    var backPoint = new StringBuilder();
    foreach (var point in points)
    {
        backPoint.AppendLine(point.ToString());
    }
    return backPoint.ToString();
});

app.Run();

//定义point类型
record Point
{
    public double X { get; set; }
    public double Y { get; set; }
    public static bool TryParse(string? pointStr, out Point? point)
    {
        if (pointStr is null)
        {
            point = null;
            return false;
        }
        point = new Point
        {
            X = double.Parse(pointStr.Split(',')[0]),
            Y = double.Parse(pointStr.Split(',')[1])
        };
        return true;
    }
}

  用postman测试:

 

 

  想要更快更方便的了解相关知识,可以关注微信公众号 

 

 

 

posted @ 2022-11-30 20:02  桂素伟  阅读(136)  评论(0编辑  收藏  举报