Advertisement
ivandrofly

asp.net: UseRouting vs UseEndPoints

Aug 5th, 2023
1,664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. var builder = WebApplication.CreateBuilder(args);
  2.  
  3. // Add services to the container.
  4.  
  5. // builder.Services.AddControllers();
  6. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  7.  
  8. var app = builder.Build();
  9.  
  10. // app.Use((context, next) =>
  11. // {
  12. //     context.Response.WriteAsync("Hello from nextflix");
  13. // });
  14.  
  15. // terminal middleware
  16. // https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-7.0
  17. // app.Run(async context =>
  18. // {
  19. //     await context.Response.WriteAsync("Hello from netflix");
  20. // });
  21.  
  22.  
  23. // case #1
  24. // app.MapGet("/prefix", async context =>
  25. // {
  26. //     var header = context.Request.Headers;
  27. //     var customHeader = header["CustomHeader"].ToString();
  28. //     var mullVad = header["VPN"].ToString();
  29. //     await context.Response.WriteAsync($"Welcome to netflix : {customHeader}\r\n");
  30. //     await context.Response.WriteAsync($"VNP client : {mullVad}");
  31. // });
  32.  
  33. app.MapGet("/", async context =>
  34. {
  35.     var headers = context.Request.Headers;
  36.     var header = context.Request.Headers["CustomHeader"].ToString();
  37.     await context.Response.WriteAsync($"Welcome to netflix: {header}");
  38. });
  39.  
  40. /// ===============================
  41. // required because of UseEndpoints
  42. // case #2
  43. app.UseRouting(); // this is the endpoint finder (will look for the best match in the list of registered endpoints)
  44. // use .UseEndpoints requires adding app.UseRouting()
  45. app.UseEndpoints(routeBuilder =>
  46. {
  47.     // registering the endpoints to the list
  48.     routeBuilder.MapGet("/prefix", async context =>
  49.     {
  50.         await context.Response.WriteAsync("hello");
  51.     });
  52. });
  53. // NOTE: If this code is enabled then the it will run before the terminal-middleware
  54. // if it's commented and the case #1 is uncommented, then the terminal middleware will run before the .MapGet()
  55. // the reason is that since there is no app.UseRouting and app.UseEndpoint() before app.Run(async context) aka terminal-middleware
  56. // the case #1 endpoint won't be called.
  57.  
  58. // # Definition
  59. // app.UseRouting: Adds a EndpointRoutingMiddleware middleware to the specified IApplicationBuilder.
  60. // A call to UseRouting(this IApplicationBuilder) must be followed by a call to UseEndpoints(this IApplicationBuilder, Action<IEndpointRouteBuilder>) for the same IApplicationBuilder instance.
  61.  
  62. // app.UseRouting: Adds a EndpointMiddleware middleware to the specified IApplicationBuilder with the
  63. // EndpointDataSource instances built from configured IEndpointRouteBuilder. The EndpointMiddleware will execute the Endpoint associated with the current request.
  64.  
  65.  
  66. // ===============================
  67.  
  68. // terminal middleware
  69. // Run delegates don't receive a next parameter. The first Run delegate is always terminal and terminates the pipeline.
  70. // Run is a convention. Some middleware components may expose Run[Middleware] methods that run at the end of the pipeline
  71. // https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-7.0#create-a-middleware-pipeline-with-webapplication
  72. app.Run(async context =>
  73. {
  74.     await context.Response.WriteAsync("Hello");
  75. });
  76. // NOTE: If the terminal-middleware is enable, your endpoint above registered with "MapGet" (case #1) won't run
  77. // since this is the only middleware and also the terminal middleware when the request first come in it will just write and return.
  78. // BUT if the case #2 is called before this terminal-middleware (which requires calling app.UseRouting and also app.UseEndpoints)
  79. // then this middleware will run after out registered endpoint runs!
  80.  
  81. // START THE APP (NOT SAME AS TERMINAL MIDDLEWARE)
  82. app.Run();
  83.  
  84.  
  85. // IMPORTANT: THE MIDDLE-WARE ARE REGISTERED IN ORDER YOU CALL app.Use<....>
  86. // e.g:
  87. // app.UseLogging();
  88. // app.UseDecorator();
  89.  
  90. // app.Run() (start the web-server) => listen for incoming http request
  91. // when arrives calls => UseLogging => UseDecorator (if there are  no more middlewares return)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement