{"id":638,"date":"2019-09-25T15:51:42","date_gmt":"2019-09-25T06:51:42","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=638"},"modified":"2019-09-25T15:51:42","modified_gmt":"2019-09-25T06:51:42","slug":"middleware-2","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=638","title":{"rendered":"middleware-2"},"content":{"rendered":"\n<pre class=\"lang:c# decode:true \" >using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder\r\nusing Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder\r\nusing Microsoft.AspNetCore.Http; \/\/WriteAsync\r\nusing Microsoft.AspNetCore; \/\/webHost\r\nusing System.IO;\r\nusing System.Threading.Tasks;\r\n\r\n\r\nnamespace WebApplication1\r\n{\r\n    public class Startup\r\n    {\r\n        public void Configure(IApplicationBuilder app)\r\n        {\r\n            \/\/We use it the type so it is clear. We will use the shortened version from now on.\r\n            \/\/app.Use(next =&gt; context =&gt; FilterAsync(context, next));\r\n            app.Use((RequestDelegate next) =&gt;\r\n            {\r\n                return (HttpContext context) =&gt; BufferAsync(context, next);\r\n            });\r\n\r\n            app.Run(async context =&gt;\r\n            {\r\n                context.Response.Headers.Add(\"content-type\", \"text\/html\");\r\n                await context.Response.WriteAsync(\"&lt;h1&gt;This sample uses buffering&lt;\/h1&gt;\");\r\n                await context.Response.WriteAsync(\"&lt;p&gt;This allows all your Middlewares to write to Response.&lt;\/p&gt;\");\r\n            });\r\n        }\r\n\r\n        public async Task BufferAsync(HttpContext context, RequestDelegate next)\r\n        {\r\n            var body = context.Response.Body;\r\n            var buffer = new MemoryStream();\r\n            context.Response.Body = buffer;\r\n\r\n            try\r\n            {\r\n                await context.Response.WriteAsync(\"&lt;html&gt;&lt;body&gt;\");\r\n                await next(context);\r\n                await context.Response.WriteAsync(\"&lt;\/body&gt;&lt;\/html&gt;\");\r\n\r\n                buffer.Position = 0;\r\n                await buffer.CopyToAsync(body);\r\n            }\r\n            finally\r\n            {\r\n                context.Response.Body = body;\r\n            }\r\n        }\r\n    }\r\n\r\n    public class Program\r\n    {\r\n        public static void Main(string[] args)\r\n        {\r\n            CreateWebHostBuilder(args).Build().Run();\r\n        }\r\n\r\n        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;\r\n            WebHost.CreateDefaultBuilder(args)\r\n                .UseStartup&lt;Startup&gt;()\r\n                .UseEnvironment(\"Development\");\r\n    }\r\n}<\/pre>\n<p>\uacb0\uacfc : This sample uses buffering<\/p>\n<p>This allows all your Middlewares to write to Response.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder using Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder using Microsoft.AspNetCore.Http; \/\/WriteAsync using Microsoft.AspNetCore; \/\/webHost using System.IO; using System.Threading.Tasks; namespace WebApplication1 { public class Startup { public void Configure(IApplicationBuilder app) { \/\/We use it the type so it is clear. We will use the shortened version from now on. \/\/app.Use(next =&gt; context =&gt; FilterAsync(context, next)); app.Use((RequestDelegate next) =&gt;\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=638\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108],"tags":[],"class_list":["post-638","post","type-post","status-publish","format-standard","hentry","category-net-core"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=638"}],"version-history":[{"count":1,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions"}],"predecessor-version":[{"id":639,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions\/639"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}