Enabling HTML files to be processed by the asp.net pipeline

Posted by on Oct 10, 2013 in Asp.net

A quick how-to here. I recently needed to process all HTML files through the ASP.Net pipeline so I could take advantage of its security features. The obvious solution to this would be to run all modules through the pipeline by adding the following to your web.config: C# <modules runAllManagedModulesForAllRequests="true" /> 1 <modules runAllManagedModulesForAllRequests="true" /> However, there would be a performance hit then on other static file types such as CSS, JavaScript and images. The correct way to achieve this is through the following web.config changes: C# <system.web> <compilation debug="true" targetFramework="4.0"> <buildProviders> <add extension =".html" type="System.Web.Compilation.PageBuildProvider"/> <add extension =".htm" type="System.Web.Compilation.PageBuildProvider"/> </buildProviders> </compilation> </system.web> 12345678 <system.web>  <compilation debug="true" targetFramework="4.0">    <buildProviders>      <add extension =".html" type="System.Web.Compilation.PageBuildProvider"/>      <add extension =".htm" type="System.Web.Compilation.PageBuildProvider"/>    </buildProviders>  </compilation></system.web> This tells the runtime that the .htm and .html extensions should be picked up and processed by the PageBuildProvider via System.Web.dll Then, to ensure this is correctly processed by IIS or IISExpress (or any other asp.net capable web server), add the following section, also to your web.config file: C# <system.webServer> <handlers> <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/> <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/> </handlers> </system.webServer> 12345678 <system.webServer>        <handlers>            <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG"                 type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/>            <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG"                 type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/>        </handlers>    </system.webServer> The above handlers instruct the runtime to run all requests for GET, POST, HEAD or DEBUG verbs through the PageHandlerFactory. With this in place you can then handle HTML files through the pipeline, which is ideal for scenarios such as integrating the asp.net security architecture into static sites containing HTML files rather than a WebForms or MVC...

Read More »