如何在ASP.NET v6中将路由移动到单独的文件



在ASP.NET for.NET 6中,可以在Program.cs(如app.MapGet("/name", YourDelegate)(中添加路由。是否可以将其移动到单独的文件中并包含到Program.cs中?

将其添加到程序中

Apis.Apis.GetApis(app);
app.Run();

并创建一个包含类Apis的单独文件,例如

namespace Apis
{
public static partial class Apis
{
public static void GetApis(WebApplication app)
{

app.MapGet("/", () => "Hello World!");
app.MapGet("/api/clients", () => new Client()
{
Id = 1,
Name = "Client 1"
});
app.MapGet("/api/clients/{id:int}", (int id) => new Client()
{
Id = id,
Name = "Client " + id
});
}
}
public  class Client
{
public int Id { get; set; }
public string Name { get; set; }
}
}

您可以创建需要的任意多个文件

相关内容

最新更新