MediaHandler should not append headers after headers have been written

Found in

EPiServer.CMS.Core 11.16.0

Fixed in

EPiServer.CMS.Core 11.18.0


Jun 25, 2020


Aug 11, 2020


CMS Core


Closed, Fixed and tested


We should not try to append headers when headers have already been sent to client since that will only cause an exception "Cannot redirect after HTTP headers have been sent".

Steps to reprduce:
1. Upload two images like "image1.png" and "image2.png" to e.g. global assets
2. Add an initialization module like:
public class RedirectMediaTest : IInitializableModule
public void Initialize(InitializationEngine context)
context.Locate.Advanced.GetInstance<IContentRouteEvents>().RoutedContent += (o, e) =>
if (e.RoutingSegmentContext.RequestUrl.ToString().EndsWith("image1.png"))

{ HttpContext.Current.Response.BufferOutput = false; HttpContext.Current.Response.Redirect(e.RoutingSegmentContext.RequestUrl.ToString().Replace("image1.png", "image2.png"), false); }



public void Uninitialize(InitializationEngine context)

3. Make a request in browser for image1.png
4. look in the log4net log

Expected result: no errors
Actual result: An excption "Cannot redirect after HTTP headers have been sent" has been thrown