Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between app.Map and app.UseEndpoints + endpoints.Map?

Looks like following two are working:

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");
    });
});

What's the difference between these two ways of mapping?

like image 605
Deqing Avatar asked Oct 30 '25 14:10

Deqing


1 Answers

app.Map doesn't use routing, it's a starts with simple string comparison. The order of the middleware is important, there's no composition model (maps run in order) and there's no support for parameters or more complex filtering logic.

The other Map (endpoint routing) is the routing system so it composes with other routes registered. This supports parameters, ordering, constraints and other extensibility. Read more about routing here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0

like image 138
davidfowl Avatar answered Nov 02 '25 04:11

davidfowl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!