ASP NET Core Middleware 1680267109
ASP NET Core Middleware 1680267109
NET Core
Middleware
In 8 Minutes!
Keivan Damirchi
Include:
● What is Middlewares?
● Request and Response Processing
● Authentication Middleware
● Logging Middleware
● Compression Middleware
● Caching Middleware
● CORS Middleware
● Costume Middleware
● Tips for working with middleware
Let's go
What is
Middleware?
In ASP.NET Core, a middleware is a component that sits
between the web server and your application, processing
incoming HTTP requests and outgoing responses.
Note
Middleware pipeline is a sequence of middleware components that are
executed in order to process an incoming HTTP request and generate a
response.
1
Flexible HTTP Request
and Response
Processing
When an HTTP request is received by the web server, it
passes through the middleware pipeline, and each
middleware component has the opportunity to inspect and
modify the request and response objects.
Note
keep each middleware component small and focused, and to use
existing middleware components when possible.
2
Authentication
Middleware
app.UseAuthentication()
This middleware is responsible for authenticating the user by
checking their credentials, such as a username and password,
against a user database. It can be used to ensure that only
authorized users can access certain pages or resources in
your application.
Tip
While middleware can be a powerful tool for adding functionality to your
application, don't overuse it. Only add middleware that is necessary for your
application's functionality.
To read more:
https://dev.to/dotnet/authentication-in-asp-net-core-59k8
3
Logging
Middleware
app.UseLogging()
This middleware is used to log incoming requests and
outgoing responses, as well as other relevant information
such as response times and error messages. This is useful
for monitoring and debugging your application, as well as
for performance analysis and auditing.
Tip
The order in which you add middlewares to the pipeline matters, as each middleware component
can modify the request and response objects before passing them on to the next middleware.
Make sure you add middlewares in the correct order to achieve the desired behavior.
To read more:
https://www.infoworld.com/article/3625494/how-to-use-http-logging-in-aspnet-core-6.html
4
Compression
Middleware
app.UseResponseCompression()
This middleware compresses outgoing responses to reduce the
size of data that is sent to the client, resulting in faster page
load times and reduced bandwidth usage. This is especially
useful for applications that serve large amounts of data, such as
images or videos.
This middleware compresses outgoing responses to reduce the size of data that is
sent to the client, resulting in faster page load times and reduced bandwidth usage.
This is especially useful for applications that serve large amounts of data, such as
images or videos.
Tip
Use existing middleware when possible: ASP.NET Core provides a number of built-in
middleware components for common tasks, such as authentication and logging. Use these when
possible instead of writing your own custom middleware.
To read more:
https://canro91.github.io/2020/10/01/CompressResponses
5
Caching
Middleware
app.UseResponseCaching()
This middleware caches the response of frequently
accessed requests, reducing the load on your server and
improving response times. This is useful for applications
that serve static content or data that doesn't change
frequently.
Tip
If your middleware requires dependencies, such as a database context or a logger, use
dependency injection to inject these dependencies into the middleware component.
To read more:
https://code-maze.com/aspnetcore-response-caching
6
CORS
Middleware
app.UseCors()
This middleware enables Cross-Origin Resource Sharing
(CORS), which allows web pages from different domains
to access resources from your application. This is useful
for building web applications that consume data from
multiple sources, such as APIs or third-party services.
Tip
Each middleware should be responsible for a specific task, such as authentication or
logging. Don't try to do too much in a single middleware component, as this can lead to
code complexity and maintenance issues.
To read more:
https://geeksarray.com/blog/how-to-setup-cors-policies-in-aspnet-core-web-api
7
Costume
Middleware
You can create your own middleware, In this example, we've created a custom
middleware called ‘MyMiddleware’. It takes a RequestDelegate parameter in its
constructor, which is a reference to the next middleware in the pipeline. The
InvokeAsync method is where the middleware logic is implemented.
To use the middleware, you need to add it to the middleware pipeline in your Startup.cs file:
8
Close();
Keivan Damirchi