We know components are added to the request pipeline using Startup.Configure()
. We understand that the middleware request pipeline is a sequence in which each delegate performs its processing and then calls the next component to do the same or short circuits the pipeline. Since the pipeline components are called in sequence, the order in which they are called is very important and is determined by where the component appears in the Startup.Configure()
method. Let’s dive in and see what effect the ordering of components has on the application.
One way to clearly demonstrate the importance of this order is when dealing with privileged information. Imagine going to your bank’s website to check your account balance. If you request the account summary page, your bank will require you to authenticate before providing the requested information. The account summary page required you to be routed through authentication before returning the information. Order matters.
In much the same way, the order of methods in Startup.Configure()
determines the order in which components are run in the middleware request pipeline of a .NET web application. Adding components in the incorrect order will affect how the application handles requests. If a user needs to be authorized before viewing certain pages of a website, we want to make sure the UseAuthorization()
method is included in the pipeline before the requested page is rendered.
The default pathway sends each HTTP request through UseHttpsRedirection()
, then UseStaticFiles()
, UseRouting()
, then UseAuthorization()
, and finally, UseEndpoints()
.
Instructions
The middleware components need to be added in the correct order for the pipeline to effectively process an HTTP request. Add the middleware components in the Configure()
method before app.UseEndpoints
so that they appear in the order as shown below:
UseHttpsRedirection
UseStaticFiles
UseRouting
UseAuthorization