Making custom modifications to output HTML

You can use this approach if you need to make changes to the output HTML code. To achieve that, use the OnBeforeFiltering and OnAfterFiltering events. The events must be connected to the processing of the requests on which you want to make the modifications.

Note that these modifications can significantly affect site performance.

  1. Assign a handler to the OutputFilterContext.CurrentFilter.OnBeforeFiltering or OnAfterFiltering event.

    • The example uses a custom class in the App_Code folder to register the event handlers.
    • The example makes use of the PostMapRequestHandler.Execute event to create an output filter instance on every request.



using System;
using CMS.OutputFilter;
using CMS.Helpers;

[FilteringHandler]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Registers the module
    /// </summary>
    private class FilteringHandler : CMSLoaderAttribute
    {
        /// <summary>
        /// Initializes the module
        /// </summary>
        public override void Init()
        {
            // Ensures that the an output filter instance is created on every request
            RequestEvents.PostMapRequestHandler.Execute += PostMapRequestHandler_Execute;
        }

        private void PostMapRequestHandler_Execute(object sender, EventArgs e)
        {
            // Creates an output filter instance
            ResponseOutputFilter.EnsureOutputFilter();

            // Assigns a handler to the OutputFilterContext.CurrentFilter.OnAfterFiltering event
            OutputFilterContext.CurrentFilter.OnAfterFiltering += CurrentFilter_OnAfterFiltering;
        }

    }

}


  1. In the FilteringHandler class, define the handler method.

    
    
    
     void CurrentFilter_OnAfterFiltering(ResponseOutputFilter filter, ref string finalHtml)
     {
         // Process the resulting HTML
         ...
    
     }
    
    
    
     
  2. Save the class.

Each request will now be processed and the output HTML will be modified by the CurrentFilter_OnAfterFiltering method.