C#C
C#4y ago
Anton

❔ ASP.NET Core how to let a controller be mapped only in development mode

This is pretty easy to do with minimal API's, you just do a check and map only if it succeeds:
app.UseEndpoints(endpoints =>
{
    if (app.Environment.IsDevelopment())
    {
        endpoints.Map("whatever", () => "blah");
    }
});

However, as I've noticed, minimal endpoints have somewhat different semantics than normal controller actions. For example, they can't match catch all routes. So the following won't work:
endpoints.Map("/{**catchAll}", async (HttpContext http, string catchAll) => "whatever");

With controllers I think it does work.
Minimal API's specificity is quite weird too. They'd match a request even having a less specific route than some of the controllers. It's as though they're treated completely separately, idk.
Was this page helpful?