---
title: Making custom modifications to output HTML
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

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 have to 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](https://docs.kentico.com/k9/custom-development/handling-global-events.md) 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.

     ```csharp

     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;
             }

         }

     }

     ```
2. In the **FilteringHandler** class, define the handler method.

   ```csharp

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

   }


   ```
3. Save the class.

Each request will now be processed and the output HTML will be modified by the CurrentFilter\_OnAfterFiltering method.
