ASP.NET Core 5 與 6 在中間件添加路由

  • 1274
  • 0

ASP.NET Core 5 與 6 在中間件添加路由

1. ASP.NET Core 5 寫法

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/", () => "Hello World");
        });
    }
}
  • 在asp.net core 5中需要使用app.UseRouting();app.UseEndpoints

2. ASP.NET Core 6 之後的寫法(MiniAPI)

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();
  • 在asp.net core 6新的寫法也稱為MiniAPI則是直接使用app.MapGet定義路由及資料即可