---
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).

By assigning custom handlers to the **OnBeforeFiltering** and **OnAfterFiltering** events, you can make changes to the output HTML code. The events must be connected to the processing of the requests on which you want to make the modifications.

> **Note:** **Note**: Output customizations can significantly affect site performance.

[Assign a handler](https://docs.kentico.com/k12sp/custom-development/handling-global-events.md) to the **OutputFilterContext.CurrentFilter.OnBeforeFiltering** or **OnAfterFiltering** event.

- The example uses a [custom module class](https://docs.kentico.com/k12sp/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) 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;
using CMS.DataEngine;
using CMS.Base;
using CMS.OutputFilter;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(OutputFilteringModule))]

public class OutputFilteringModule : Module
{
    // Module class constructor, the system registers the module under the name "CustomOutputFiltering"
    public OutputFilteringModule()
        : base("CustomOutputFiltering")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

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

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

```

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