IIS 7.0 Response Modification Framework

————————–

For more information, see the Feb 2008 edition of MSDN Magazine: Build Web Server Solutions with End-To-End Extensibility.

————————–

What is the response modification framework?

It is a fancy term for an IIS 7.0 module that modifies the content returned by a web application on the fly, without the need to modify the application itself. The Response Modification Framework  (RMF) allows you to configure rules that apply various transformations to the responses generated by your application, including regular expression based replacements, stripping whitespace, and more.

For example, you can use RMF rules to replace hyperlinks, dynamically inject headers or footers into your pages, redirect images, and much more.  You can do simply by defining configuration rules that use built-in replacement filters like the String or Regular Expression replacements, without writing a single line of code.  Alternatively, you can quickly build your own custom transformations for use with your application.

I built the Response Modification Framework as an example of end-to-end extensibility provided by IIS 7.0. You can find an in-depth walkthrough of how RMF is built in the February edition of MSDN magazine (link coming soon). In the article, I use RMF to insert a header and footer into my QDIG image gallery, and then inject a little custom script that preloads gallery images as compressed preview images first for speed of viewing before loading the full image:

Response Modification in Action (MSDN magazine)

 

I’ll also be posting more information, and the source code for the project shortly. In the meantime, you can download a preview of RMF and a sample application here.

When should I use response modification?

You should use response modification whenever it is not feasible to make the changes you need to make to the application itself.  In some cases, modifying the responses on the fly is the only option – for example, if your content includes static files that need to be modified with a header/footer.

Modifying the application responses on the fly may negatively impact the performance of your application – whether or not it’s acceptable depends on many factors including the average response sizes, the extent of transformations, and your performance goals.

How do I use the Response Modification Framework in my application?

Using the Response Modification Framework is simple:

1.       Just drop the module into your application’s /BIN directory (or GAC it)

2.       Register the <responseModification> configuration section in your application’s web.config file (or your framework’s root web.config file)

3.       Create a few replacement rules in configuration

Here is an example of replacing all occurrences of “Roger” with “Tiger” using a regular expression replacement rule:

  <responseModification enabled="true">

    <add name="SorryYouTigerNow"

        conditionType="Mvolo.ResponseFilter.Conditions.ContentTypeCondition"

        condition="text/html"

        replaceType="Mvolo.ResponseFilter.Filters.RegExReplace"

        replace="Roger"

        replaceWith="Tiger"

        options="IgnoreCase"

  />

  </responseModification>

For more examples, download the sample application.

The Response Modification Framework also comes with a plug-in for IIS Manager that you can use to manage the response modification rules in your application:

IIS Manager plugin for the Response Modification Framework 

To use it, you have to do the following:

1.       GAC Mvolo.ResponseFilter.dll, Mvolo.ResponseFilterUI.Server.dll, and Mvolo.ResponseFilterUI.Client.dll

2.       Deploy the responsefilter_schema.xml schema file, and register the responseModification configuration section at the server level instead of in the app’s web.config

3.       Add the “Mvolo.ResponseFilter.UI.Server.ResponseFilterModuleProvider, Mvolo.ResponseFilterUI.Server, Version=1.0.0.0, Culture=neutral, PublicKeyToken=895e90205a14f2aa” entry to the <moduleProviders> and <modules> sections in administration.config.

For more step by step info, see the MSDN magazine article.

How do I do custom transformations?

The “framework” part of RMF means that the module itself provides its own extensibility model – which allows you to quickly build your own replacement filters.

For example, some of the first filters I built for it include an XSLT transformation filter for rendering XML files (which I used it to serve my Failed Request Tracing XML logs using the IIS 7.0 stylesheet), and an bulk replacement filter that uses string TRIEs to efficiently do mass dictionary word replacements.  

The Response Modification Framework abstracts away all of the complexity of dealing with response filtering, and lets you focus on the actual transformations your want to make. All you need to do is take the response as a string, make changes to it, and return another string containing your changes. That’s it.

Building a filter is easy as pie. Here is a sample Lower-Case filter:

class LowerCase : StringRespo
nseFilter

{

    public override string ReplaceResponse(HttpContext context, string currentResponse)

    {

        if (!String.IsNullOrEmpty(currentResponse))

        {

            return currentResponse.ToLower();

        }

        else

        {

            return null;

        }

    }

}

Then, drop the source file into the /App_Code directory or the compiled version into the /BIN directory of your web application (or register it in the GAC), and create a replacement rule for it in the configuration:

<responseFilter enabled="true">

  <add name="LowerCase"

      conditionType="ContentType"

      condition="text/html"

      replaceType="MyFilters.LowerCase"

  />

</responseFilter>

You can also implement custom conditions that determine when your replacement rule should be in effect.  To do this, simply implement the Mvolo.ResponseFilter.Condition class, and use it in your response modification rule.

—————————–

The RMF is curently available as a demo application, which contains the RMF preview. Version 1.0 is coming shortly along with the source code. NOTE: This is released under the Microsoft Permissive License.

[Preview release: Download the RMF sample application ]

——————————-

That’s it for now. This is just a high-level summary, I will be posting more info, examples, and source code shortly.

Thanks,

Mike

4 Comments

  1. John Halliday

    I’d be interested to know if you ever released the source for your Request Modification Framework assembly? Or if you completed it?

  2. John Halliday

    Hi Mike,

    I’d like to know if you ever completed and release the Response Modification Framework? We would like to look at using something like this.

Leave a Reply

Your email address will not be published. Required fields are marked *