In the new ASP.NET 5.0 (vNext), the startup code relies on the IApplicationBuilder
interface. The Use
method is used to add a handler to the builder, while Build
is used to construct the final delegate. But I can't figure out what is the purpose of New
. I've been digging in GitHub, but can't find any place where that's used.
Anyone understand what is the purpose of that method?
New()
creates a second ApplicationBuilder
, sharing all the ApplicationServices
and ServerFeatures
of the first one, but none of the middleware. It is used internally by the branching extensions (Map
, MapWhen
, UseWhen
) to create the new 'branch'.
You can find the implementation here: ApplicationBuilder.cs.
In some cases, it is also useful in higher-level frameworks.
For exemple, the [MiddlewareFilter]
attribute in MVC Core uses New()
internally to execute a piece of ASP.NET Core middleware inside the MVC framework (i.e. as a filter). MVC Core creates a small pipeline around the middleware, builds it into a RequestDelegate, then runs the HttpContext through it. Just like ASP.NET Core does with your 'main' pipeline built in Startup.cs
.
Thanks to this feature, we can reuse a piece of general-purpose ASP.NET Core middleware, from inside MVC.
For more information, see MiddlewareFilterBuilder.cs in ASP.NET MVC Core.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With