/// <summary> /// Inspects all non WebSocket content for HTML documents /// and if it finds HTML injects the JavaScript needed to /// refresh the browser via Web Sockets. /// /// Uses a wrapper stream to wrap the response and examine /// only text/html requests - other content is passed through /// as is. /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task HandleHtmlInjection(HttpContext context) { using (var filteredResponse = new ResponseStreamWrapper(context.Response.Body, context)) { context.Response.Body = filteredResponse; await _next(context); } }
/// <summary> /// Inspects all non WebSocket content for HTML documents /// and if it finds HTML injects the JavaScript needed to /// refresh the browser via Web Sockets. /// /// Uses a wrapper stream to wrap the response and examine /// only text/html requests - other content is passed through /// as is. /// </summary> /// <param name="context"></param> /// <returns></returns> private async Task HandleHtmlInjection(HttpContext context) { var path = context.Request.Path.Value; // Use a custom StreamWrapper to rewrite output on Write/WriteAsync using (var filteredResponse = new ResponseStreamWrapper(context.Response.Body, context)) { // Use new IHttpResponseBodyFeature for abstractions of pilelines/streams etc. // For 3.x this works reliably while direct Response.Body was causing random HTTP failures context.Features.Set <IHttpResponseBodyFeature>(new StreamResponseBodyFeature(filteredResponse)); await _next(context); } }