net5.0快速Fast Api

Featherhttp

github:https://github.com/featherhttp/framework

示例:https://github.com/featherhttp/tutorial

FeatherHttp 是一个轻量级的服务器端框架,.NET Core 应用程序的轻量级低仪式 api

代码:

Program:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;


static async Task Main(string[] args)
    {
        var app = WebApplication.Create(args);

        app.MapGet("/api/todos", GetTodos);
        app.MapGet("/api/todos/{id}", GetTodo);
        app.MapPost("/api/todos", CreateTodo);
        app.MapPost("/api/todos/{id}", UpdateCompleted);
        app.MapDelete("/api/todos/{id}", DeleteTodo);

        await app.RunAsync();
    }

    static async Task GetTodos(HttpContext http)
    {
        using var db = new TodoDbContext();
        var todos = await db.Todos.ToListAsync();

        await http.Response.WriteJsonAsync(todos);
    }

    static async Task GetTodo(HttpContext http)
    {
        if (!http.Request.RouteValues.TryGet("id", out int id))
        {
            http.Response.StatusCode = 400;
            return;
        }

        using var db = new TodoDbContext();
        var todo = await db.Todos.FindAsync(id);
        if (todo == null)
        {
            http.Response.StatusCode = 404;
            return;
        }

        await http.Response.WriteJsonAsync(todo);
    }

    static async Task CreateTodo(HttpContext http)
    {
        var todo = await http.Request.ReadJsonAsync<TodoItem>();

        using var db = new TodoDbContext();
        await db.Todos.AddAsync(todo);
        await db.SaveChangesAsync();

        http.Response.StatusCode = 204;
    }

    static async Task UpdateCompleted(HttpContext http)
    {
        if (!http.Request.RouteValues.TryGet("id", out int id))
        {
            http.Response.StatusCode = 400;
            return;
        }

        using var db = new TodoDbContext();
        var todo = await db.Todos.FindAsync(id);

        if (todo == null)
        {
            http.Response.StatusCode = 404;
            return;
        }

        var inputTodo = await http.Request.ReadJsonAsync<TodoItem>();
        todo.IsComplete = inputTodo.IsComplete;

        await db.SaveChangesAsync();

        http.Response.StatusCode = 204;
    }

    static async Task DeleteTodo(HttpContext http)
    {
        if (!http.Request.RouteValues.TryGet("id", out int id))
        {
            http.Response.StatusCode = 400;
            return;
        }

        using var db = new TodoDbContext();
        var todo = await db.Todos.FindAsync(id);
        if (todo == null)
        {
            http.Response.StatusCode = 404;
            return;
        }

        db.Todos.Remove(todo);
        await db.SaveChangesAsync();

        http.Response.StatusCode = 204;
    }

TodoDbContext:

using Microsoft.EntityFrameworkCore;

public class TodoDbContext : DbContext
{
    public DbSet<TodoItem> Todos { get; set; }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseInMemoryDatabase("Todos");
    }
}

TodoItem:

using System.Text.Json.Serialization;

public class TodoItem
{
    [JsonPropertyName("id")]
    public int Id { get; set; }
    [JsonPropertyName("name")]
    public string Name { get; set; }
    [JsonPropertyName("isComplete")]
    public bool IsComplete { get; set; }
}

 

运行项目:

dotnet watch run

 

访问:http://localhost:5000/api/todos  没有数据,返回空数组

至此一个简单快速的服务端Api搭建完成

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值