Last active
May 9, 2016 17:29
-
-
Save StephanyBatista/4bdd053ff1ff4a1a3bf3bf0dd799b806 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNet.Builder; | |
using Microsoft.AspNet.Http; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Threading.Tasks; | |
namespace MiddlewareArticle.Middleware | |
{ | |
public class ResponseTime | |
{ | |
RequestDelegate _next; | |
public ResponseTime(RequestDelegate next) | |
{ | |
_next = next; | |
} | |
public async Task Invoke(HttpContext context) | |
{ | |
var sw = new Stopwatch(); | |
sw.Start(); | |
await _next(context); | |
var isHtml = context.Response.ContentType?.ToLower().Contains("text/html"); | |
if (context.Response.StatusCode == 200 && isHtml.GetValueOrDefault()) | |
{ | |
var body = context.Response.Body; | |
using (var streamWriter = new StreamWriter(body)) | |
{ | |
var textHtml = string.Format( | |
"<footer><div id='process'>Response Time {0} milliseconds.</div>", | |
sw.ElapsedMilliseconds); | |
streamWriter.Write(textHtml); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment