Middleware, Run Vs Use, Custom middleware in ASP.NET Core

 namespace ASPCoreMiddlewares

{

    public class Program

    {

        public static void Main(string[] args)

        {

            var builder = WebApplication.CreateBuilder(args);

            var app = builder.Build();


            //app.MapGet("/", () => "Hello World!");


            //app.Run() method is used to define middleware

            //Run() method will not execute next subsequent middleware

            //Below is custom middleware

            //app.Run(async (context) =>

            //{

            //    await context.Response.WriteAsync("Welcome to ASP.NET Core\n");

            //});


            //app.Use() method provides facility to call next middleware

            app.Use(async (context, next) => 

            {

                await context.Response.WriteAsync("Welcome to ASP.NET Core\n");

                await next(context);

            });


            app.Use(async (context,next) => 

            {

                await context.Response.WriteAsync("Programmentor\n");

                await next(context);

            });


            app.Run(async (context) => 

            {

                await context.Response.WriteAsync("Hello Viewers...");

            });


            app.Run();

        }

    }

}










Comments

Popular posts from this blog

Map, MapGet, MapPost, MapPut and MapDelete methods in ASP.NET Core MVC - Routing ASP.NET

Layout View In ASP.NET Core 6 | _Layout.cshtml ASP.NET | Master Page ASP.Net Core

Attribute Routing in ASP.NET Core MVC