Can static html pages be post-processed in IIS?

1.7k views Asked by At

There's a WebForms project, with a bunch of aspx and html pages. I want to postprocess an html markup to be rendered in an HttpModule in a similar way like here. For aspx pages that works.

For html pages, however, when I try to read from Request.InputStream, it's empty, Request.InputStream.Length is equal 0. What do I do wrong, is it possible to change markup for html pages on the fly using ASP.NET engine?

Background info: I want to conditionally remove some html elements depending on the user permissions.

UPDATE: According to the comments, I updated web.config but can't get the desired result. When I hit /my.html page, the request comes to my module and I'm able to read cookies, headers, but that's not what I want. I want to read then contents of the html page and write the updated version into the context.Response. I tried to run the webapp in IIS and Cassini just in case (integrated and classic mode).

public void Init(HttpApplication context)
    {
        context.PostRequestHandlerExecute += ContextOnPostRequestHandlerExecute;
    }

    private void ContextOnPostRequestHandlerExecute(object sender, EventArgs eventArgs)
    {
        var app = sender as HttpApplication;
        if (app == null)
        {
            return;
        }

        var url = app.Request.Url;
        if (url.AbsolutePath.EndsWith(".html"))
        {
            var length = app.Request.ContentLength; // always equal 0
        }
    }

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
  <authentication mode="None" />
  <compilation debug="true" targetFramework="4.5">
  <buildProviders>
    <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
  </buildProviders>
</compilation>
<httpModules>
  <add name="MyModule" type="TestWebApp.MyModule, TestWebApp" />
</httpModules>
<httpRuntime targetFramework="4.5" />
<pages>
  <namespaces>
    <add namespace="System.Web.Optimization" />
    <add namespace="Microsoft.AspNet.Identity" />
  </namespaces>
  <controls>
    <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
  </controls>
</pages>
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
  </providers>
</sessionState>
</system.web>

<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
  <remove name="FormsAuthentication" />
  <add name="PostprocessHtmlModule" type="TestWebApp.MyModule, TestWebApp" preCondition="integratedMode" />
</modules>
<handlers>
  <add name="html" verb="GET, HEAD, POST, DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
</handlers>
</system.webServer>
</configuration>
1

There are 1 answers

2
Ross Bush On

Make sure you let the web server know that it should use the asp.net pipeline to handle the static content. By default, the static pages will not be processed by your app. Something like:

Here is the config that has been tested and I know works.

<system.web>
    <compilation>
        <buildProviders>
        <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
        </buildProviders>
    </compilation>
</system.web>

<system.webServer>
    <handlers>
        <add name="html" verb="GET, HEAD, POST, DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
    </handlers>
</system.webServer>