某些请求未调用httpmodule

时间:2015-11-24 23:01:47

标签: c# asp.net web-config iis-8 httpmodule

我是ASP Net编程的新手,我正在尝试创建一个自定义HttpModule来阻止MS-DOS设备名称(AUX / CON / prn / clock $ / NUL / COM1 / LPT1 / LPT2 / COM2 / COM3 / COM4 /C:。)。我在IIS 8.0中工作。

首先,我尝试创建一个测试模块(直接来自msdn教程,介绍如何创建模块)

https://msdn.microsoft.com/library/ms227673(v=vs.100).aspx

using System;
using System.Web;
public class HelloWorldModule : IHttpModule
{
    public HelloWorldModule()
    {
    }

    public String ModuleName
    {
        get { return "HelloWorldModule"; }
    }

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest += 
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += 
            (new EventHandler(this.Application_EndRequest));
    }

    private void Application_BeginRequest(Object source, 
         EventArgs e)
    {
    // Create HttpApplication and HttpContext objects to access
    // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = 
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx"))
        {
            context.Response.Write("<h1><font color=red>" +
                "HelloWorldModule: Beginning of Request" +
                "</font></h1><hr>");
        }
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = 
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx"))
        {
            context.Response.Write("<hr><h1><font color=red>" +
                "HelloWorldModule: End of Request</font></h1>");
        }
    }

    public void Dispose() { }
}

我在web.config

中有这个
<configuration>
  <system.webServer>
    <modules>
      <add name="HelloWorldModule" type="HelloWorldModule"/>
    </modules>
  </system.webServer>
</configuration>

因此,当我输入正常请求(例如localhost / Hello)时,我的测试模块会被调用,但是当我输入诸如localhost / AUX之类的请求时,它不会被调用。而是转到服务器错误页面。

当我输入请求localhost / AUX时,是否需要设置任何配置才能调用模块?

谢谢!

0 个答案:

没有答案