app.Map 和 app.UseEndpoints + endpoints.Map 之间的区别?

Deq*_*ing 3 c# asp.net asp.net-core

看起来以下两个正在工作:

app.UseRouting();

// http://localhost/apple
app.UseEndpoints(endpoints =>
{
    endpoints.Map("/apple", async context =>
    {
        await context.Response.WriteAsync("this is an apple");
    });
});

// http://localhost/orange
app.Map("/orange", orangeApp =>
{
    orangeApp.Run(async context =>
    {
        await context.Response.WriteAsync("this is an orange");
    });
});
Run Code Online (Sandbox Code Playgroud)

这两种映射方式有什么区别?

dav*_*owl 6

app.Map不使用路由,它是从简单的字符串比较开始的。中间件的顺序很重要,没有组合模型(地图按顺序运行),并且不支持参数或更复杂的过滤逻辑。

另一个Map(端点路由)是路由系统,因此它与注册的其他路由组成。这支持参数、排序、约束和其他可扩展性。在此处了解有关路由的更多信息https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0