asp.net自定义模块没有被加载?

时间:2012-06-28 14:04:48

标签: c# asp.net asp.net-mvc module httpmodule

我基于两篇文章创建了一个自定义模块:

http://community.devexpress.com/blogs/paulk/archive/2009/03/30/implementing-an-ihttpmodule.aspx

该模块基于:

How do I display the duration it took to generate a page in a pages footer?

我添加了以下课程:

public class PerformanceMonitorModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += delegate(object sender, EventArgs e)
        {
            //Set Page Timer Star
            HttpContext requestContext = ((HttpApplication)sender).Context;
            Stopwatch timer = new Stopwatch();
            requestContext.Items["Timer"] = timer;
            timer.Start();
        };
        context.PostRequestHandlerExecute += delegate(object sender, EventArgs e)
        {

            HttpContext httpContext = ((HttpApplication)sender).Context;
            HttpResponse response = httpContext.Response;
            Stopwatch timer = (Stopwatch)httpContext.Items["Timer"];
            timer.Stop();

            // Don't interfere with non-HTML responses
            if (response.ContentType == "text/html")
            {
                double seconds = (double)timer.ElapsedTicks / Stopwatch.Frequency;
                string result_time = string.Format("{0:F4} sec ", seconds);
                RenderQueriesToResponse(response, result_time);
            }
        };
    }
    void RenderQueriesToResponse(HttpResponse response, string result_time)
    {
        response.Write("<div style=\"margin: 5px; background-color: #FFFF00\"");
        response.Write(string.Format("<b>Page Generated in " + result_time));
        response.Write("</div>");
    }
    public void Dispose() { /* Not needed */ }
} // End of PerformanceMonitorModule

以下是我的web.config:

<system.web>
  ...
  <httpModules>
    <add name="PerformanceMonitorModule" type="PerformanceMonitorModule"/>
  </httpModules>
</system.web>

我在PerformanceMonitorModule类init方法中设置了断点,它永远不会被调用。

一些注意事项:

  • 网站正在使用HTTPS运行。我不认为这会有所作为吗?
  • 该网站正在Azure计算机模拟器中运行。不确定这是否会导致任何问题。
  • 我尝试过汇编限定名称(MySite.Website.Business.Modules.PerformanceMonitorModule,MySite.Website,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null)。

我不知道还有什么可以尝试才能加载这个模块?

0 个答案:

没有答案