有一个 WebForms 项目,有一堆 aspx 和 html 页面。我想以与此处类似的方式对要在 HttpModule 中呈现的 html 标记进行后处理。对于有效的 aspx 页面。
但是,对于 html 页面,当我尝试从 读取时Request.InputStream
,它是空的,Request.InputStream.Length
等于 0。我做错了什么,是否可以使用 ASP.NET 引擎动态更改 html 页面的标记?
背景信息:我想根据用户权限有条件地删除一些 html 元素。
更新:
根据评论,我更新了 web.config 但无法获得预期的结果。当我点击 /my.html 页面时,请求到达我的模块,我可以读取 cookie、标头,但这不是我想要的。我想阅读 html 页面的内容并将更新的版本写入context.Response
. 为了以防万一(集成和经典模式),我尝试在 IIS 和 Cassini 中运行 webapp。
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
}
}
网络配置:
<?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>