Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
60 views

Learn ASP - NET: ASP - NET: Middleware Cheatsheet

The document discusses middleware in ASP.NET, including how middleware components are added and ordered in the pipeline, how to access built-in and add custom middleware, and how different middleware components like UseExceptionHandler, UseHttpsRedirection, and UseStaticFiles work.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Learn ASP - NET: ASP - NET: Middleware Cheatsheet

The document discusses middleware in ASP.NET, including how middleware components are added and ordered in the pipeline, how to access built-in and add custom middleware, and how different middleware components like UseExceptionHandler, UseHttpsRedirection, and UseStaticFiles work.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cheatsheets / Learn ASP.

NET

ASP.NET: Middleware

Understanding ASP.NET 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 ASP.NET Middleware Components


The Configure() method is called from the
Startup class. Configure() calls various
app.UseX() methods to build the middleware
pipeline.

Ordering ASP.NET 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
Startup.Configure() method.

Accessing Built-in ASP.NET 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 app.Use(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 context.Response.WriteAsync("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 ASP.NET middleware


IApplicationBuilder.Run() is a app.Run(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 context.Response.WriteAsync("Terminal
delegates after this component will not be executed. middleware!");
});

Understanding ASP.NET 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 ASP.NET 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 ASP.NET UseExceptionHandler Component
The UseExceptionHandler() component if (env.IsDevelopment())
can be used in production environments to catch
app.UseDeveloperExceptionPage();
and log errors and route users to a general error
page without exposing sensitive details about the else
application. The UseExceptionHandler() app.UseExceptionHandler("/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 ASP.NET 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 ASP.NET Endpoints


UseRouting() , must be called to compare the app.UseRouting();
HTTP request with the available endpoints and
app.UseEndpoints(endpoints => {
decide which is the best match.
UseEndpoints() then calls endpoints.MapRazorPages();
MapRazorPages() with a lambda expression });
to register the endpoint, and then executes the
selected delegate that matches our HTTP request.

Understanding the ASP.NET 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 ASP.NET 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

You might also like