Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118
Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119
Note: the issue referenced in this post has been fixed and will get released with ASP.NET Core 1.1.
BestASPNETHostingReview.com | Best and cheap ASP.NET Core 1.1 Hosting. In this post I will explain you to using ASP.NET Core’s middleware to modify response body. While creating a small piece of middleware to modify the response body’s content, sometimes we had trouble getting anything to appear in my browser. I would get a 200
response-code, but no content (i.e. Content-Length: 0
). Here is an example that creates a similar setup:
Startup.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.Use(async (context, next) => { var newContent = string.Empty; using (var newBody = new MemoryStream()) { // We set the response body to our stream so we can read after the chain of middlewares have been called. context.Response.Body = newBody; await next(); // Reset the body so nothing from the latter middlewares goes to the output. context.Response.Body = new MemoryStream(); newBody.Seek(0, SeekOrigin.Begin); // newContent will be `Hello`. newContent = new StreamReader(newBody).ReadToEnd(); newContent += ", World!"; // Send our modified content to the response body. await context.Response.WriteAsync(newContent); } }); app.Map("", configuration => { configuration.Use(async (context, next) => { await context.Response.WriteAsync("Hello"); }); }); } } |
[su_button url=”http://asphostportal.com/ASPNET-1-Hosting.aspx” style=”stroked” background=”#eab912″ size=”4″]Best and Recommended ASP.NET Core 1.1 Hosting Click Here !![/su_button]
Initial response
1 2 3 4 5 6 7 | curl -i http://localhost:54122/ HTTP/1.1 200 OK Server: Kestrel X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcd2lsbGlhbS1ib2dhLWl2XERvY3VtZW50c1xwcm9qZWN0c1wxMGstYXBhcnRcc3JjXDEwa0FwYXJ0?= X-Powered-By: ASP.NET Date: Tue, 4 Oct 2016 22:42:51 GMT Content-Length: 0 |
Turns out this is a known issue and has an easy workaround which involves storing the original response stream and setting back to it after the middlewares are called:
Updated Startup.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.Use(async (context, next) => { var newContent = string.Empty; // Store the "pre-modified" response stream. var existingBody = context.Response.Body; using (var newBody = new MemoryStream()) { // Previous code removed for brevity. await next(); // Set the stream back to the original. context.Response.Body = existingBody; newBody.Seek(0, SeekOrigin.Begin); // Previous code removed for brevity. } }); // Previous code removed for brevity. } } |
New response
1 2 3 4 5 6 7 8 9 | curl -i http://localhost:54122/ HTTP/1.1 200 OK Transfer-Encoding: chunked Server: Kestrel X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcd2lsbGlhbS1ib2dhLWl2XERvY3VtZW50c1xwcm9qZWN0c1wxMGstYXBhcnRcc3JjXDEwa0FwYXJ0?= X-Powered-By: ASP.NET Date: Tue, 4 Oct 2016 22:49:59 GMT Hello, World! |
I hope this article helpful for you, happy coding 🙂