2

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>
myroman
  • 532
  • 5
  • 11
  • Are the 'static' pages ASPX pages that cannot be modified or strictly the HTML pages? – user2864740 Jun 23 '15 at 17:29
  • It's not ASP.NET unless it's .. in ASP.NET, which HTML pages are not handled under if they are 'static'. – user2864740 Jun 23 '15 at 18:16
  • @myroman Yes... similarly answered here (same topic, different question, so not voting to close as duplicate): http://stackoverflow.com/a/21970733/1810243. or similar response with a custom handler http://stackoverflow.com/a/19124733/1810243 – MikeSmithDev Jun 23 '15 at 18:19

1 Answers1

1

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>
Ross Bush
  • 14,648
  • 2
  • 32
  • 55
  • I added this, it didn't work neither for nor , Request.InputStream.Length = 0. Also tried to add , error is still there. Not sure what to do at this stage. Should I set mimeType up? – myroman Jun 23 '15 at 20:47
  • It should already be there. I would google httphandler for static .html files. It may be a typo in the config. This is one way http://forums.iis.net/t/1177995.aspx – Ross Bush Jun 23 '15 at 20:53