---
title: "Explain the difference between app.Run and app.Use in ASP.NET Core."  
description: "Explain the difference between app.Run and app.Use in ASP.NET Core."  
author: "Revati S Misra"  
published: 2023-03-06  
updated: 2023-07-08  
canonical: https://www.mindstick.com/forum/157450/explain-the-difference-between-app-run-and-app-use-in-asp-dot-net-core  
category: "asp.net core"  
tags: ["asp.net", "asp.net core"]  
reading_time: 2 minutes  

---

# Explain the difference between app.Run and app.Use in ASP.NET Core.

[Explain the difference](https://www.mindstick.com/forum/156125/can-you-explain-the-difference-between-organic-and-paid-results) between app.Run and app.Use in [ASP.NET Core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio).

## Replies

### Reply by Aryan Kumar

The `app.run()` and `app.use()` methods are used to configure middleware in [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) 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](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) 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#

```plaintext
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#

```plaintext
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.


---

Original Source: https://www.mindstick.com/forum/157450/explain-the-difference-between-app-run-and-app-use-in-asp-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
