5

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

Konrad Viltersten
  • 28,018
  • 52
  • 196
  • 347
nesh_s
  • 369
  • 3
  • 14
  • rename your static .html page to .aspx (no change in content needed) and the thing should work – Alex Jan 20 '12 at 13:07
  • GZIP already does this http://stackoverflow.com/questions/702124/enable-iis7-gzip – jrummell Jan 20 '12 at 13:17
  • @alex - thats not what i want to achieve. i could have easily done that and not raised this question at all. i want to be able to use this httpmodule for not only asp.net applications, but cold fusion ones as well. my aim is to get it working for .html pages and take it from there. – nesh_s Jan 20 '12 at 13:20
  • @jrummell - thanks, for that. but that does not really answer my question :) i dont want to enable gzip ('cause the load balancer that we use wont support it!) – nesh_s Jan 20 '12 at 13:21

2 Answers2

3

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.

ScottE
  • 21,027
  • 18
  • 91
  • 129
  • Do you have your module registered in system.webserver > modules? otherwise it won't get picked up. and if so, please post your web.config portion – ScottE Jan 20 '12 at 13:39
  • yes.. added the system.webserver part from my web.config in the main question – nesh_s Jan 20 '12 at 13:52
  • Your web.config looks fine, except that runAllManagedModulesForAllRequests="true" is superfluous in your case as it just runs all modules as preCondition="", which you already have. Can you debug your module to make sure it's getting hit? – ScottE Jan 20 '12 at 14:13
  • yours have been the most helpful answers, but sadly i cant upvote you (i dont have enough points to do that). thanks for you help anyways – nesh_s Jan 20 '12 at 15:36
1

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

Kangkan
  • 13,787
  • 9
  • 64
  • 107