K
K
koss4ok2022-04-21 11:46:42
ASP.NET
koss4ok, 2022-04-21 11:46:42

Why have Map() when you can just branch with if/switch?

Why does the WebApplication class in ASP.NET Core have a Map() extension method?
After all, there are simple constructions like if/switch ?
Something like

app.Use(async (context, next) =>
{
    string? path = context.Request.Path.Value?.ToLower();
    if (path == "/date")
    {
        await context.Response.WriteAsync($"Date: {DateTime.Now.ToShortDateString()}");
    }
    else if (path == "/time")
    {
        await context.Response.WriteAsync($"Date: {DateTime.Now.ToShortTimeString()}");
    }
    else
    { 
        await next.Invoke();
    }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Voland69, 2022-04-21
@koss4ok

A real apishka is about a dozen controllers with at least 4 actions (CRUD), with common error handling, authentication, authorization, and so on.
Ifas and switches in a startup will have a noble footcloth, otherwise you can get by with one extension method that, when the application starts, will run through the controllers and collect the routing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question