Erik Lenaerts

Do, or do not. There is no try. - Yoda

Don't register your Http Response Filter too soon

I'm working on a Translation system for one of my customers and I used a Http response Filter. The filter basically looks for certain patterns and replaces these with new values. This way we translate some content of the pages.

The filter is registered using an HttpModule like shown in the code below. The goal of this filter is to only translate HTML pages and ignore the others like images, javascripts, css, ...

// Only apply the translation filter on html content, since content like javascrip shouldn't be 
// translated
if (_context.Response.ContentType == "text/html")
{
    
// Create a new filter and insert it onto the page
    // This filter will later on, translate all content
    
_context.Response.Filter = new TranslationFilter(_context.Response.Filter);
}             

Note that in the example code above that _context is a class level variable initialized in the Init method of the IHttpModule interface

/// <summary>
/// Inits the specified application.
/// </summary>
/// <param name="application">The current Http Application</param>
public void Init(System.Web.HttpApplication application)
{
    _context 
application;
    
//...
}

The point of this post is to underline WHEN you should register the filter. If we look at the Application Life Cycle like in the picture below. If you would register the filter during the BeginRequest as I did initially then you won't have any information on the ContentType via _context.Response.ContentType. At the BeginRequest point in time, the page isn't executed yet and hence no content type information is set. So, you need to register this later on like on the ReleaseRequestState.

ASP.Net life cycle   

(Image referenced from this article)

 

Complete code:

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace Translation
{
    
/// <summary>
    /// Translation HttpModule  
    /// </summary>
    
public class TranslationHttpModule IHttpModule
    { 
        private System.Web.HttpApplication _context

        
/// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"></see>.
        /// </summary>
        
public void Dispose() { }

        
/// <summary>
        /// Inits the specified application.
        /// </summary>
        /// <param name="application">The current Http Application</param>
        
public void Init(System.Web.HttpApplication application)
        
{
            _context 
application;
            
_context.ReleaseRequestState += new EventHandler(_context_ReleaseRequestState);
        
}

        
void _context_ReleaseRequestState(object senderEventArgs e)
        
{
            
// Only apply the translation filter on html content, since content like javascrip shouldn't be 
            // translated
            
if (_context.Response.ContentType == "text/html")
            
{
                
// Create a new filter and insert it onto the page
                // This filter will later on, translate all content
                
_context.Response.Filter = new TranslationFilter(_context.Response.Filter);
            
}            
        } 
    }
}

Comments

Erik Lenaerts said:

Suppose you want to translate content by replacing tokens with new words that you for example maintain

# July 19, 2007 9:36 AM

Erik Lenaerts said:

This post continues from part one . In this previous post we created a Http Response Filter that translated

# July 21, 2007 6:11 AM

Erik Lenaerts said:

This post continues from part one . In this previous post we created a Http Response Filter that translated

# July 21, 2007 6:13 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: