コード例 #1
0
        public void Configure(IApplicationBuilder app)
        {
            // MAP
            app.Map("/now", now =>
            {
                now.Run(async context =>
                {
                    var time = DateTime.UtcNow.ToString("HH:mm:ss (UTC)");
                    await context
                    .Response
                    .WriteAsync("<h1 style='color:red;'>" + time + "</h1>");
                });
            });

            // MAPWHEN
            app.MapWhen(
                context => context.Request.Query.ContainsKey("utc"),
                utc =>
            {
                utc.Run(async context =>
                {
                    var time = DateTime.UtcNow.ToString("HH:mm:ss (UTC)");
                    await context
                    .Response
                    .WriteAsync("<h1 style='color:blue;'>" + time + "</h1>");
                });
            });

            // BEFORE/AFTER
            app.Use(async(context, nextMiddleware) =>
            {
                await context.Response.WriteAsync("BEFORE");
                await nextMiddleware();
                await context.Response.WriteAsync("AFTER");
            });

            // BODY SIZE
            app.Use(async(context, nextMiddleware) =>
            {
                context.Features
                .Get <IHttpMaxRequestBodySizeFeature>()
                .MaxRequestBodySize = 10 * 1024;
                await nextMiddleware.Invoke();
            });

            // MOBILE
            // app.UseMiddleware<MobileDetectionMiddleware>();
            app.UseMobileDetection();

            app.Run(async(context) =>
            {
                var obj = new SomeWork();
                await context
                .Response
                .WriteAsync("<h1 style='color:red;'>" + obj.Now() + "</h1>");
            });
        }
コード例 #2
0
        public void Configure(IApplicationBuilder app)
        {
            // Add a sample response header
            app.Use(async(context, nextMiddleware) =>
            {
                context.Response.OnStarting(() =>
                {
                    context.Response.Headers.Add("Book", "Programming ASP.NET Core");
                    return(Task.FromResult(0));
                });
                await nextMiddleware();
            });

            app.Use(async(context, nextMiddleware) =>
            {
                using (var memory = new MemoryStream())
                {
                    var originalStream    = context.Response.Body;
                    context.Response.Body = memory;

                    await nextMiddleware();


                    memory.Seek(0, SeekOrigin.Begin);
                    var content = new StreamReader(memory).ReadToEnd();
                    memory.Seek(0, SeekOrigin.Begin);

                    // Apply logic here for deciding which headers to add
                    context.Response.Headers.Add("Body", content);

                    await memory.CopyToAsync(originalStream);
                    context.Response.Body = originalStream;
                }
            });

            app.Run(async(context) =>
            {
                var obj = new SomeWork();
                await context
                .Response
                .WriteAsync("<h1 style='color:red;'>" + obj.Now() + "</h1>");
            });
        }