Question

I have created a simple HttpModule which removes whitespaces from response before sending it to the client. This works fine for an aspx page on IIS7.0 but if i create a static html page and call it, the HttpModule does not kick in (the way i know is because the source contains whitespaces, which otherwise should have been removed). Apparently there is something i m not doing right, but dont know what.

My website is in an Application Pool with .NET 4.0 and ManagedPipelineMode = Integrated.

I have added my module as a ManagedModule and refers to an strong-name-key assembly from GAC.

thanks

Edit- here is the system.webserver part from web.config

<system.webServer>
  ...
  <modules runAllManagedModulesForAllRequests="true">
    <add name="RemoveWhitespaceHttpModule" 
         type="HttpModules.Modules.RemoveWhitespaceHttpModule, HttpModules, 
           Version=1.0.0.0, Culture=neutral, PublicKeyToken=8a83u4bi47o9fo0d" 
           preCondition="" />
  </modules>
  <defaultDocument>
    <files>
      <add value="TestForm.aspx" />
    </files>
  </defaultDocument>
</system.webServer>

Edit- Fixed it. For anyone interested, this is how my module checks the response and then decides whether to proceed with whitespace removal or not

if (contentType.Equals("text/html") 
  && httpContext.Response.StatusCode == 200 
  && httpContext.CurrentHandler != null)
{ ... }

The problem was with the third condition above httpContext.CurrentHandler != null. when calling this module for static .html pages, the currentHandler was null and hence the code never went inside to manipulate html. i have removed this third condition and it works now. thanks for your answers everyone

Was it helpful?

Solution

This should do the trick, in the web.config:

<modules runAllManagedModulesForAllRequests="true"></modules>

This is a quick and easy solution, but can cause issues / performance issues.

OTHER TIPS

You need to look at the Handler Mapping in your IIS.

How a handler works is that on IIS, the handler is registered and supposed to handle a particuler type of page. You can look at the "Handler Mappings" in the IIS [In the run command type inetmgr and hit enter. IIS Manager will pop up and look for Handler Mappings in the IIS section.]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top