Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var builder = WebApplication.CreateBuilder(args);
- // Add services to the container.
- // builder.Services.AddControllers();
- // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
- var app = builder.Build();
- // app.Use((context, next) =>
- // {
- // context.Response.WriteAsync("Hello from nextflix");
- // });
- // terminal middleware
- // https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-7.0
- // app.Run(async context =>
- // {
- // await context.Response.WriteAsync("Hello from netflix");
- // });
- // case #1
- // app.MapGet("/prefix", async context =>
- // {
- // var header = context.Request.Headers;
- // var customHeader = header["CustomHeader"].ToString();
- // var mullVad = header["VPN"].ToString();
- // await context.Response.WriteAsync($"Welcome to netflix : {customHeader}\r\n");
- // await context.Response.WriteAsync($"VNP client : {mullVad}");
- // });
- app.MapGet("/", async context =>
- {
- var headers = context.Request.Headers;
- var header = context.Request.Headers["CustomHeader"].ToString();
- await context.Response.WriteAsync($"Welcome to netflix: {header}");
- });
- /// ===============================
- // required because of UseEndpoints
- // case #2
- app.UseRouting(); // this is the endpoint finder (will look for the best match in the list of registered endpoints)
- // use .UseEndpoints requires adding app.UseRouting()
- app.UseEndpoints(routeBuilder =>
- {
- // registering the endpoints to the list
- routeBuilder.MapGet("/prefix", async context =>
- {
- await context.Response.WriteAsync("hello");
- });
- });
- // NOTE: If this code is enabled then the it will run before the terminal-middleware
- // if it's commented and the case #1 is uncommented, then the terminal middleware will run before the .MapGet()
- // the reason is that since there is no app.UseRouting and app.UseEndpoint() before app.Run(async context) aka terminal-middleware
- // the case #1 endpoint won't be called.
- // # Definition
- // app.UseRouting: Adds a EndpointRoutingMiddleware middleware to the specified IApplicationBuilder.
- // A call to UseRouting(this IApplicationBuilder) must be followed by a call to UseEndpoints(this IApplicationBuilder, Action<IEndpointRouteBuilder>) for the same IApplicationBuilder instance.
- // app.UseRouting: Adds a EndpointMiddleware middleware to the specified IApplicationBuilder with the
- // EndpointDataSource instances built from configured IEndpointRouteBuilder. The EndpointMiddleware will execute the Endpoint associated with the current request.
- // ===============================
- // terminal middleware
- // Run delegates don't receive a next parameter. The first Run delegate is always terminal and terminates the pipeline.
- // Run is a convention. Some middleware components may expose Run[Middleware] methods that run at the end of the pipeline
- // https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-7.0#create-a-middleware-pipeline-with-webapplication
- app.Run(async context =>
- {
- await context.Response.WriteAsync("Hello");
- });
- // NOTE: If the terminal-middleware is enable, your endpoint above registered with "MapGet" (case #1) won't run
- // since this is the only middleware and also the terminal middleware when the request first come in it will just write and return.
- // BUT if the case #2 is called before this terminal-middleware (which requires calling app.UseRouting and also app.UseEndpoints)
- // then this middleware will run after out registered endpoint runs!
- // START THE APP (NOT SAME AS TERMINAL MIDDLEWARE)
- app.Run();
- // IMPORTANT: THE MIDDLE-WARE ARE REGISTERED IN ORDER YOU CALL app.Use<....>
- // e.g:
- // app.UseLogging();
- // app.UseDecorator();
- // app.Run() (start the web-server) => listen for incoming http request
- // when arrives calls => UseLogging => UseDecorator (if there are no more middlewares return)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement