dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.21k stars 9.95k forks source link

MapMiddleware extensions are missing in new Endpoint. #14514

Open pavinan opened 4 years ago

pavinan commented 4 years ago

MapMiddleware extensions are missing in new Endpoint.

In previous routebuilder it has MapMiddlewareRoute, MapMiddlewareGet, MapMiddlewarePost, MapMiddlewarePut, MapMiddlewareDelete.

I have considered the following alternative.

app.UseEndpoints(endpoints =>
{
    var newAppbuilder = endpoints.CreateApplicationBuilder();
    newAppbuilder.UseMiddleware<Middleware1>();

    endpoints.MapGet("middleware1", newAppbuilder.Build());
});
pranavkm commented 4 years ago

Thanks @pavinan. We'll investigating adding these APIs in the next major release.

shadow-cs commented 4 years ago

We have come across a similar problem. Previously you could use UseRouter and call UseMvc on both places (while configuring the IRouter ie. in the pipeline branch and on the main app branch). This is however not supported with endpoint routing (as far as I know since calling UseEndpoints on a branched app is not supported).

The way we want to solve this is by using UseWhen which splits the pipeline and rejoins:

app.UseRouting();
app.UseWhen(c => c.Request.Path.StartsWithSegments("/foo"), 
  innerApp => innerApp.UseMiddleware<FooMiddleware>()));
app.UseEndpoints(endpoints => endpoints.MapControllers())'

Is this the right way to go?

As an addition to MapMiddlewareRoute (which branches the pipeline) we would find an analogous UseMiddlewareRoute (if it would rejoin and use the MapControllers from the original branch) useful if such a construct would be possible.

Tratcher commented 4 years ago

@shadow-cs I was able to work around this today as follows:

app.UseEndpoints(endpoints =>
{
    var subApp = app.New();
    subApp.UseMiddleware<FooMiddleware>();
    endpoints.Map("/foo", subApp.Build());
}

Edit: This probably doesn't stack to let you augment existing routes like controllers though.

mkArtakMSFT commented 4 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.