示例#1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }



            app.Use(async(context, next) =>
            {
                Trace.WriteLine($"request {context.Request.Method} {context.Request.Path}");

                await next.Invoke();

                Trace.WriteLine($"response {context.Response.StatusCode}");
            });

            // app.UseMiddleware<LoggerMiddleware>();

            app.UseLogger();

            // GET /dashboard

            app.UseDashboard();

            //app.Map("/dashboard",
            //    options => options.Run(context => context.Response.WriteAsync("Hello Dashboard")));

            //   app.Map("/dashboard", DashboardHandler);

            app.Map("/radio/status",
                    options => options.Run(context =>
            {
                using IRadio radio = context.RequestServices.GetRequiredService <IRadio>();
                radio.Call(new User {
                    Name = "A"
                }, new User {
                    Name = "B"
                });
                string result = radio.IsOn.ToString();
                return(context.Response.WriteAsync(result));
            }));

            app.Map("/sensors", node =>
            {
                // switch
                node.Map("/temp",
                         options => options.Run(context => context.Response.WriteAsync("Temperature")));
                node.Map("/humidity",
                         options => options.Run(context => context.Response.WriteAsync("Humidity")));

                // default:
                node.Map(string.Empty,
                         options => options.Run(context => context.Response.WriteAsync("All sensors")));
            });


            //  app.Run(context => context.Response.WriteAsync("Hello World!"));

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });

                endpoints.MapPost("api/radios", async context =>
                {
                    context.Response.StatusCode = 201;

                    await context.Response.WriteAsync("Created!");
                });

                endpoints.MapGet("api/radios/{number:int}", GetRadioHandler);
            });
        }