IIS7自定义HttpModule的网站 - 如何
问题描述:
我一直在网上搜索如何创建一个HttpModule, 的例子,因为我想在网站中使用我正在开发atm。 我发现的所有例子都很简单,但很难实现,然后我在开始。IIS7自定义HttpModule的网站 - 如何
这里是一个样本的HttpModule:
public class SampleModule : IHttpModule
{
DateTime beginrequest;
public void Init(HttpApplication app)
{
// register for events created by the pipeline
app.BeginRequest += new EventHandler(this.OnBeginRequest);
app.EndRequest += new EventHandler(this.OnEndRequest);
}
public void Dispose() { }
public void OnBeginRequest(object o, EventArgs args)
{
// obtain the time of the current request
beginrequest = DateTime.Now;
}
public void OnEndRequest(object o, EventArgs args)
{
// get the time elapsed for the request
TimeSpan elapsedtime = DateTime.Now - beginrequest;
// get access to application object and the context object
HttpApplication app = (HttpApplication)o;
HttpContext ctx = app.Context;
// add header to HTTP response
ctx.Response.AppendHeader("ElapsedTime", elapsedtime.ToString());
}
}
实现模块本来应该像这样(由所有的例子我已经找到):
<configuration>
<system.web>
<httpModules>
<add name="TimeElapsedModule" type="SampleModules.SampleModule,SampleModules "/>
</httpModules>
</system.web>
</configuration>
但IIS7这个心不是吧!在IIS7你应该做的:
<system.webServer>
<modules>
<add name="TimeElapsedModule" type="SampleModules.SampleModule,SampleModules"/>
</modules>
</system.webServer>
现在,搞清楚了这一点后,我遇到了,我似乎没有找到一个解决的另一个问题。 我收到以下错误:
Could not load file or assembly 'SampleModules' or one of its dependencies. The system cannot find the file specified.
这是合乎逻辑的,因为我的生活模块另一个类里面。 如果有人能够帮助我解决这个问题,我会很高兴,我希望这篇文章能够帮助他们的研究员在他们需要的日子。
答
原来我使用的是命名空间类型,而不是使用命名空间限定的类名。在我的web.config换句话说,我不得不
<add type="SampleModules.SampleModule,SampleModules" name="TimeElapsedModule" />
,而不是
<add type="Test.SampleModules.SampleModule,App_Code" name="TimeElapsedModule" />