The app.run() and app.use() methods are used to configure middleware in ASP.NET Core applications. Middleware is a piece of code that is executed for every request that comes into the application. It can be used to perform tasks such as authentication, authorization, and logging.
The app.run() method is used to configure middleware that will be executed for every request. The
app.use() method is used to configure middleware that will be executed for specific requests.
The main difference between the two methods is that the app.run() method is executed once, at the beginning of the request pipeline, while the
app.use() method is executed multiple times, once for each request that matches the specified route.
Here is an example of how to use the app.run() method to configure middleware that will be executed for every request:
C#
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace MyApp
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
// This middleware will be executed for every request
context.Response.Headers.Add("X-Powered-By", "ASP.NET Core");
await context.Response.WriteAsync("Hello, world!");
});
}
}
}
Here is an example of how to use the app.use() method to configure middleware that will be executed for specific requests:
C#
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace MyApp
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Use(async (context) =>
{
// This middleware will be executed for requests that match the '/api/products' route
if (context.Request.Path == "/api/products")
{
context.Response.Headers.Add("X-API-Product-Version", "1.0");
}
});
app.Run(async (context) =>
{
// This middleware will be executed for every request
context.Response.Headers.Add("X-Powered-By", "ASP.NET Core");
await context.Response.WriteAsync("Hello, world!");
});
}
}
}
In general, you should use the app.run() method to configure middleware that needs to be executed for every request. You should use the
app.use() method to configure middleware that needs to be executed for specific requests.
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.
The
app.run()andapp.use()methods are used to configure middleware in ASP.NET Core applications. Middleware is a piece of code that is executed for every request that comes into the application. It can be used to perform tasks such as authentication, authorization, and logging.The
app.run()method is used to configure middleware that will be executed for every request. Theapp.use()method is used to configure middleware that will be executed for specific requests.The main difference between the two methods is that the
app.run()method is executed once, at the beginning of the request pipeline, while theapp.use()method is executed multiple times, once for each request that matches the specified route.Here is an example of how to use the
app.run()method to configure middleware that will be executed for every request:C#
Here is an example of how to use the
app.use()method to configure middleware that will be executed for specific requests:C#
In general, you should use the
app.run()method to configure middleware that needs to be executed for every request. You should use theapp.use()method to configure middleware that needs to be executed for specific requests.