copy了官方的代码
using System; using System.Web; public class Moduletest : IHttpModule { public Moduletest() { } 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
<?xml version="1.0"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5"/> <httpRuntime targetFramework="4.5"/> <httpModules> <add name="Moduletest" type="Moduletest"/> </httpModules> </system.web> </configuration>
新增一个Default.aspx页面
在Page_Load方法添加一句代码
protected void Page_Load(object sender, EventArgs e) { Response.Write("<br/><br/>来自Default.aspx页面<br/>"); }
运行出错
如果把web.config文件里的注册语句
<httpModules> <add name="Moduletest" type="Moduletest"/> </httpModules>
删除掉,运行就没问题。
求大牛看看到底什么问题?
你是在集成模式下运行的,不支持这种配置,把IIS应用程序池改成经典模式就行了。如图,选择Classic
另外,也可以修改配置文件。具体可以参考http://www.jb51.net/article/31010.htm里面说的。
1、图片看不清楚。
2、干嘛不调试下?
3、Module的type是否加上模块更好?