Cheatsheets / Learn ASP.
NET
[Link]: Middleware
Understanding [Link] middleware
Web applications use a series of components to
route each HTTP request to its destination and then
return an appropriate response to the user. This
series of components is organized in a pipeline
which is collectively known as middleware.
Adding [Link] Middleware Components
The Configure() method is called from the
Startup class. Configure() calls various
[Link]() methods to build the middleware
pipeline.
Ordering [Link] Middleware
Middleware components are called in sequence. The
order in which components are called is very
important and is determined by where the
component appears in the
[Link]() method.
Accessing Built-in [Link] Components
The IApplicationBuilder interface public void
de!nes the built-in middleware methods. These
Configure(IApplicationBuilder app,
methods are de!ned by using the format UseX()
with X describing the action performed by the
IWebHostEnvironment env)
method.
Adding custom middleware components
The IApplicationBuilder interface [Link](async (context, next) => {
provides a generic Use() method which can be
await
used to process custom middleware components
and call the next component in the pipeline. Multiple [Link]("Custom
middleware components can be chained together middleware!");
with the Use() method, which accepts two await next();
parameters, the HTTP context and the next
middleware component to be called.
});
Adding terminal [Link] middleware
[Link]() is a [Link](async (context) => {
terminal middleware component which begins
await
returning the HTTP response. Run() can be
added to the end of the request pipeline since any [Link]("Terminal
delegates after this component will not be executed. middleware!");
});
Understanding [Link] Nested Structure
Proper ordering of middleware components is
important. The middleware request pipeline has a
nested structure where each component can
perform operations before and after the next
component.
Understanding [Link] Developer Exception Pages
UseDeveloperExceptionPage() is a
method which provides an exception page
speci!cally designed for developers. The method
should be placed before any middleware
components that are catching exceptions. Some of
the information displayed includes the stack trace,
any query string parameters, cookies, headers, and
routing information.
Understanding [Link] UseExceptionHandler Component
The UseExceptionHandler() component if ([Link]())
can be used in production environments to catch
[Link]();
and log errors and route users to a general error
page without exposing sensitive details about the else
application. The UseExceptionHandler() [Link]("/Error");
component has several overloads, but a simple way
of using it is to pass in the name of the page (as a
string) that should display if an exception is thrown.
Redirecting Requests with [Link] Middleware
The UseHttpsRedirection() method is
the middleware component used to capture HTTP
requests and redirect them to the more secure
HTTPS protocol. Since the redirection is done in the
app con!guration, the user never even has to know
the redirection occurred.
Enabling [Link] Endpoints
UseRouting() , must be called to compare the [Link]();
HTTP request with the available endpoints and
[Link](endpoints => {
decide which is the best match.
UseEndpoints() then calls [Link]();
MapRazorPages() with a lambda expression });
to register the endpoint, and then executes the
selected delegate that matches our HTTP request.
Understanding the [Link] UseStaticFiles Component
Static !les make up an important part of the
application — they often contain HTML, CSS, and
JavaScript code that controls how the application
looks and behaves. The UseStaticFiles()
method is available to ensure static !le content is
rendered alongside the HTML for our web
applications.
Understanding the [Link] UseAuthorization Component
The UseAuthorization() component
checks the user’s request against their authorization
status. If the authorization check passes, this
component will pass the request to the next
component in the pipeline. Otherwise, it will short-
circuit the pipeline and either present the user with
a login page or an error.
Save Print Share