HTTP Module for Javascript application IIS



IISHTTP 模块只能为 IIS 中的 ASP.Net/MVC 应用程序配置吗?因为我有 Angular 2 应用程序(仅限 HTML 和 js)并将其部署在 IIS 中,所以它运行良好,但我需要在访问 Angular 应用程序 URL 时读取请求标头。

因此,我正在考虑创建一个HTTP模块,如此链接所述

在我的 Angular 应用程序 web.config 文件中,创建了一个模块元素,如下所示。

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
<modules>
<add name="MyModule" type="SiteMinderTokenReader.MyModule" />
</modules>
</system.webServer>
</configuration>

但模块未按预期工作

这是我的模块代码。

namespace SiteMinderTokenHandler
{
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication httpApplication = (HttpApplication)sender;
httpApplication.Context.Response.Write("<h1>The header....</h1>");
}
void context_EndRequest(object sender, EventArgs e)
{
HttpApplication httpApplication = (HttpApplication)sender;
httpApplication.Context.Response.Write("<h6>The footer....</h6>");
}
}
}

您的模块不会运行,因为您正在提供静态内容。为了使模块针对所有内容运行,您需要添加此属性。

<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="SiteMinderTokenReader.MyModule"/>
</modules>

runAllManagedModulesForAllRequests="true">

这将为所有请求运行您的模块,甚至是静态内容。另外,请确保您的"类型"正确。包含完整的命名空间。

相关内容

最新更新