Middleware is a piece of code that is executed for every request that comes into an ASP.NET Core application. It can be used to perform tasks such as authentication, authorization, and logging.
Middleware is implemented as a class that implements the IMiddleware interface. The
IMiddleware interface has a single method, Invoke(), which is called for every request. The
Invoke() method takes a HttpContext object as its parameter. The
HttpContext object contains information about the request, such as the URL, the headers, and the body of the request.
The Invoke() method can return one of three values:
Task: This indicates that the middleware has completed its processing and the request should be processed by the next middleware in the pipeline.
Task<IActionResult>: This indicates that the middleware has completed its processing and has returned an
IActionResult object. The IActionResult object will be used to determine the response that is sent to the client.
void: This indicates that the middleware has not completed its processing and will call the
Invoke() method of the next middleware in the pipeline.
The middleware pipeline is a sequence of middleware that is executed for every request. The middleware pipeline is executed in the order that the middleware is added to the application.
The following is an example of a middleware class:
C#
using Microsoft.AspNetCore.Http;
namespace MyApp
{
public class HelloWorldMiddleware : IMiddleware
{
public async Task Invoke(HttpContext context)
{
// Write a message to the response
context.Response.WriteAsync("Hello, world!");
// Continue processing the request
await context.NextMiddleware();
}
}
}
This middleware class simply writes a message to the response and then continues processing the request.
To add middleware to an ASP.NET Core application, you can use the app.Use() method in the
Configure() method of the Startup class. The app.Use() method takes a middleware class as its parameter.
The following is an example of how to add the HelloWorldMiddleware class to an ASP.NET Core application:
C#
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace MyApp
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
// Add the HelloWorldMiddleware class to the pipeline
app.Use(new HelloWorldMiddleware());
// Continue configuring the application
app.Run(async (context) =>
{
// ...
});
}
}
}
Once you have added middleware to your application, it will be executed for every request. You can use middleware to perform a variety of tasks, such as authentication, authorization, and logging.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Middleware is a piece of code that is executed for every request that comes into an ASP.NET Core application. It can be used to perform tasks such as authentication, authorization, and logging.
Middleware is implemented as a class that implements the
IMiddlewareinterface. TheIMiddlewareinterface has a single method,Invoke(), which is called for every request. TheInvoke()method takes aHttpContextobject as its parameter. TheHttpContextobject contains information about the request, such as the URL, the headers, and the body of the request.The
Invoke()method can return one of three values:Task: This indicates that the middleware has completed its processing and the request should be processed by the next middleware in the pipeline.Task<IActionResult>: This indicates that the middleware has completed its processing and has returned anIActionResultobject. TheIActionResultobject will be used to determine the response that is sent to the client.void: This indicates that the middleware has not completed its processing and will call theInvoke()method of the next middleware in the pipeline.The middleware pipeline is a sequence of middleware that is executed for every request. The middleware pipeline is executed in the order that the middleware is added to the application.
The following is an example of a middleware class:
C#
This middleware class simply writes a message to the response and then continues processing the request.
To add middleware to an ASP.NET Core application, you can use the
app.Use()method in theConfigure()method of theStartupclass. Theapp.Use()method takes a middleware class as its parameter.The following is an example of how to add the
HelloWorldMiddlewareclass to an ASP.NET Core application:C#
Once you have added middleware to your application, it will be executed for every request. You can use middleware to perform a variety of tasks, such as authentication, authorization, and logging.