_From @guylando on January 11, 2018 12:19_
I can't find an example which shows what is the correct order for all the following middleware when used together. It would be nice if there would be a maintained startup.cs containing ALL possible middleware.
UseStatusCodePagesWithRedirects
UseStaticFiles
UseAuthentication
UseResponseCaching
UseWebSockets
UseSignalR
UseResponseCompression
UseMvc
Would save a lot of time and errors having it.
Thanks!
_Copied from original issue: aspnet/Home#2769_
There is no definitive order, only some relative dependencies between individual components. E.g. StatusCodes should come before components that generate responses without bodies. StaticFiles are often early in the pipeline for efficiency, there can be a lot of them. Auth should come before components that require auth, like MVC. Caching should come before components that generate cacheable responses like MVC. WebSockets should come before components that use them like SignalR. Compression should come before components that generate compressable responses, and after caching if you want to cache the compressed version.
Maybe what we need to explain is if a middleware handles requests as they arrive, or if it inserts itself to react to responses as they're generated. E.g. caching and compression attach to a request as it comes in and monitor the response to see if they need to react. StaticFiles by contract handles any requests it can immediately.
_From @guylando on January 11, 2018 23:15_
Thanks, Maybe a dependency tree\graph can be built\maintained in the documentation instead of one startup.cs because the possibilities are better described in a tree\graph than in a linear code? Such tree\graph would probably be very useful for many
Even a table might help as a quick reference:
Middleware | Ordering
---------------|----------
UseStatusCodePagesWithRedirects | Before components that set status codes
UseAuthentication | Before HttpContext.User is needed. Terminal for OAuth callbacks.
UseResponseCaching | Before components that want caching
UseWebSockets | Before components that want to use WebSockets
UseResponseCompression | Before components that want to use compression
UseStaticFiles | Terminal if a request matches files
UseSignalR | Terminal for matching routes / hubs
UseMvc | Terminal if a request matches routes / controllers & actions
Possibly sorted by pass through vs terminal. That sorting alone would give you a sense of the ordering.
_From @guylando on January 12, 2018 16:45_
That's great! thanks
@Tratcher How about I add the table to this doc? https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs=aspnetcore2x
Yes that looks like the right doc. Try adding a column to the existing table in built-in middleware, with a reference to the ordering section.
Most helpful comment
Even a table might help as a quick reference:
Middleware | Ordering
---------------|----------
UseStatusCodePagesWithRedirects | Before components that set status codes
UseAuthentication | Before HttpContext.User is needed. Terminal for OAuth callbacks.
UseResponseCaching | Before components that want caching
UseWebSockets | Before components that want to use WebSockets
UseResponseCompression | Before components that want to use compression
UseStaticFiles | Terminal if a request matches files
UseSignalR | Terminal for matching routes / hubs
UseMvc | Terminal if a request matches routes / controllers & actions
Possibly sorted by pass through vs terminal. That sorting alone would give you a sense of the ordering.