首页 新闻 赞助 找找看

根据官方实例操作Module报错

0
悬赏园豆:10 [已解决问题] 解决于 2015-04-01 22:40

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> 

删除掉,运行就没问题。


求大牛看看到底什么问题?


        
网子的主页 网子 | 初学一级 | 园豆:167
提问于:2015-04-01 00:16
< >
分享
最佳答案
0

你是在集成模式下运行的,不支持这种配置,把IIS应用程序池改成经典模式就行了。如图,选择Classic

 

 

另外,也可以修改配置文件。具体可以参考http://www.jb51.net/article/31010.htm里面说的。

收获园豆:10
bulusli | 菜鸟二级 |园豆:331 | 2015-04-01 16:38
其他回答(1)
0

1、图片看不清楚。

2、干嘛不调试下?

3、Module的type是否加上模块更好?

519740105 | 园豆:5810 (大侠五级) | 2015-04-01 10:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册