IIS7.5迁移 - HTTP 错误 404.0 - Not Found-0x80070002错误分析及解决过程

时间:2022-06-12 18:30:38

Asp.net应用迁移到IIS7.5 时,站点报告 HTTP 错误 404.0 - Not Found 错误。处理过程如下:
运行环境:Framework 2.0,IIS7.5,Web站点一套,没有源代码,应用程序池默认为经典模式.
报错页面的URL如下:http://localhost/HL2008/UI/AutoMonitor/135?autorun=true
看到这个URL地址,第一反应就是HttpModule被重写了,通过.net reflector 7.0 反编译HL2008.Web.Common.dll,发现UrlRewriter类,实现了System.Web.IHttpModule接口.
继续跟踪,这个类的核心代码就是一个跳转操作.

void context_BeginRequest(object sender, EventArgs e)
        {
            //string path = string.Format("~/Action/RequestHandler.aspx?id={0}&{1}","yeyang",HttpContext.Current.Request.RawUrl);
            //HttpContext.Current.RewritePath(path,false);
            ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Info("DemoHttpModule context_BeginRequest 被调用");

            string p = string.Empty;
            //if (GlobalDefine.IsAutoMon(out p))
            if (IsAutoMon(out p))
            {
                log.Info("DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == true 被调用");
                string path = string.Format("~/UI/AutoMonitor/AutoMonitorBasePage.aspx?id={0}&{1}", p, HttpContext.Current.Request.QueryString);
                HttpContext.Current.RewritePath(path, false);
            }
            else
            {
                log.Info("DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用");
            }

        }

 
  public static bool IsAutoMon(out string PermissionId)
        {
            PermissionId = GetPermissionId();
            return !string.IsNullOrEmpty(PermissionId);
        }
 public static string GetPermissionId()
        {
            return GetPermissionId(HttpContext.Current.Request.RawUrl);
        }
        public static string GetPermissionId(string url)
        {
            ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            log.Info("DemoHttpModule GetPermissionId 被调用");
            log.Info(string.Format("Url 地址是[{0}]", url));
            string PermissionId = string.Empty;
            MatchCollection mc = new Regex(@"\/UI\/AutoMonitor\/(?<permissionid>(\d+))(($)|(\?\S+))", RegexOptions.IgnoreCase).Matches(url);
            if (mc.Count > 0)
            {
                return mc[0].Groups["permissionid"].Value;
            }
            log.Info("在第一个正则匹配之后:mc.Count == 0");
            mc = new Regex(@"/UI/AutoMonitor/AutoMonitorBasePage.aspx\?id=(?<permissionid>\d+)", RegexOptions.IgnoreCase).Matches(url);
            if (mc.Count > 0)
            {
                PermissionId = mc[0].Groups["permissionid"].Value;
            }
            return PermissionId;
        }

因此初步判定为HttpModule没有被激活,通过网页搜索,找到一篇相近的博文(http://q.cnblogs.com/q/38592/),其中提到IIS7.5下 httpModules 使用出现:404 求解决,需要将modules配置增加到<system.webServer>配置节中.
此时的web.config中,在<system.web>下有httpModules和httpHandlers配置,<system.webServer>下有modules和handlers配置.
问题还是没有解决,通过将增加了log4net日志的modules组件插接到原有应用程序中输出的日志来分析,应用程序在没有激活http://localhost/HL2008/UI/AutoMonitor/135?autorun=true这个请求之前就已经终止了.日志如下:

 - DemoHttpModule context_BeginRequest 被调用
2015-01-12 12:41:37,010 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-12 12:41:37,010 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/UI/Security/Main.aspx/FocusHashCode]
2015-01-12 12:41:37,010 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-12 12:41:37,010 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-12 12:42:01,313 [1276] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-12 12:42:01,313 [1276] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-12 12:42:01,313 [1276] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/UI/Security/Main.aspx/FocusHashCode]
2015-01-12 12:42:01,313 [1276] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-12 12:42:01,313 [1276] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-12 12:42:06,804 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-12 12:42:06,804 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-12 12:42:06,804 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/UI/Security/Main.aspx/FocusHashCode]
2015-01-12 12:42:06,804 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-12 12:42:06,804 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-12 12:42:11,921 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-12 12:42:11,921 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-12 12:42:11,921 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/UI/Security/Main.aspx/FocusHashCode]
2015-01-12 12:42:11,921 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-12 12:42:11,921 [4084] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用

于是问题就被定义为,modules被调用了,但是并没有激活http://localhost/HL2008/UI/AutoMonitor/135?autorun=true这个请求.
继续,将应用程序池设置为集成模式,这时候IIS错误如下:
HTTP 错误 500.22 - Internal Server Error
检测到在集成的托管管道模式下不适用的 ASP.NET 设置。
模块:ConfigurationValidationModule
通知:BeginRequest
处理程序:StaticFile
错误代码:0x80070032
关键的信息如下:
最可能的原因:此应用程序在 system.web/httpModules 节中定义配置。
于是剔除了system.web/httpModules配置节
发起请求,继续HTTP错误,关键信息如下:
?此应用程序在 system.web/httpHandlers 节中定义配置。
剔除system.web/httpHandlers配置节.
发起请求.日志内容如下:

2015-01-12 22:03:18,058 [6676] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule init 被调用
2015-01-12 22:03:18,136 [6676] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-12 22:03:18,136 [6676] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-12 22:03:18,136 [6676] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/UI/AutoMonitor/135?autorun=true]
2015-01-12 22:03:18,136 [6676] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == true 被调用

部署到生产环境后,发现框架页面的JQuery框架失效了.继续分析.发现框架页面使用到了ScriptManager控件,对应生成的Html代码形如:

<script src="/HL2008/ScriptResource.axd?d=v9-Vun33Q_Tt6fz_-KdfOxFkktcmXkHtJ_xaAlnYyRI1w9v_8zmlGWWHE_7u5Jrvi61k8TbJhxrwk5pWmXh_t7arSi5GkKCo5fRbtk8AocTMmYe-Z7DxXDxZ58VlpPixyBnc59F5eZHs5UV4zWR1dzu8BAU-upWipKdmIR9lgzaYhBED0&t=fffffffff9d85fa6" type="text/javascript"></script>
<script type="text/javascript"> 

再次检查web.config文件,并在webServer中增加以下配置(灵感来自新建web项目时自动生成的代码):

<handlers>
			<remove name="WebServiceHandlerFactory-Integrated"/>
			<remove name="ScriptHandlerFactory"/>
			<remove name="ScriptHandlerFactoryAppServices"/>
			<remove name="ScriptResource"/>
			<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
			<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
			<add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>

与上述代码对应的老的web.config中的配置如下:

<httpHandlers>
      <remove verb="*" path="*.asmx" />
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
    </httpHandlers>

修改后的web.config,生成的调试日志如下:

2015-01-13 17:23:36,107 [6872] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,107 [6872] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,107 [6872] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/WebResource.axd?d=gzmZlqjdAFh6OdPGsBWp09jjWPrybcivIxB1dm9gbM8VxMISKAXu6a4U2PqImHgLFM7eAwQYHR9YWjCvEdtxcmFt2KUu6k398_VF7VKu6P0hbarkRVBG1v0HhDL3RubSr-FjRA2&t=634134071139291572]
2015-01-13 17:23:36,107 [6872] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,107 [6872] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/ScriptResource.axd?d=N98_-SqLQ5674uNkLlgKa0Owba70UUJIMsXPPAsyT8skc5Y68FcDhDOE2hZO2_Abfm4CJI36eipVEOKx_qvvj5tECsi8ECd2UEcZevylFPlnvkifyGrPKKvzZONnzCSOA-CDsOISCtqpIQkZHcFG1b-8QFs1&t=434e8d85]
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/ScriptResource.axd?d=v9-Vun33Q_Tt6fz_-KdfOxFkktcmXkHtJ_xaAlnYyRI1w9v_8zmlGWWHE_7u5Jrvi61k8TbJhxrwk5pWmXh_t7arSi5GkKCo5fRbtk8AocTMmYe-Z7DxXDxZ58VlpPixyBnc59F5eZHs5UV4zWR1dzu8BAU-upWipKdmIR9lgzaYhBED0&t=fffffffff9d85fa6]
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule init 被调用
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/ScriptResource.axd?d=cNixn-OEPWiQvFR3nFBwBrScpOjRsGp3fK5-H9KtsGrmb5eRvELjuTbsfhOOw80UrKbNl6SQ8-0hshzPc4OU2ynAKKiUHOR-vFpJxtiDz2fm420esofumH2wQGbp5wRVQjPsut_vVJsyCltqoJO2yiIOgA_iWolrD9KQNUVR6IBhOY3U0&t=434e8d85]
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,170 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/ScriptResource.axd?d=GkKeUdwTdRtNhm1Ma1oioEFAgffAgAW_IXjB82oPb2TS8P8ORcSnRzj-mMUHz0DqTyxBwcApwJHW6TtsGFJAg9GUe_PAIQks-Mb5WHwblXUn3u_FGjbGRGIjGbLZP5C05BIa63bIg9F1gYs-gw_xTr8IgDU1&t=434e8d85]
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,170 [11096] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/ScriptResource.axd?d=iHAPcT_FVgUzSSekKo0HhOH348ApF9q6M46cXylkg7WB62P5SDfYGAEH8pzg6XDXyS7COBUUOw4Fi-UMAJxKG_ADQ3oE0UkqOshzqC1PcDtV8tbAQJqIN7dSKhFMq7naNJRSkw2&t=434e8d85]
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/WebResource.axd?d=5IPS5otIYO6UJXlB39gOLqh4nEWaX7eoKOZvuIHqSQGUss98IRLKepKPUlZKDz8l9IfSSht6J2qLr8DGuKecSvF1LBM1&t=634259354654344891]
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是<span style="color:#ff0000;">[/HL2008/ScriptResource.axd?d=B0mz6YxA5JQOuXpa0JogyXkEuiSqJXT0qhalr9oqWikr61b8HSaJNPPLphYdrWAcRD2qqxeovdT1vMJ6ZJAoyN4NSKI5R6CqEBMiDY_-KpQ9Q6lkll0eG-QfovrUW1f2A4xfJkZnlNqRzbVG0GKMOqqst59KbZ_eEFL25pKyFJr9Zh-90&t=fffffffff9d85fa6</span>]
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - 在第一个正则匹配之后:mc.Count == 0
2015-01-13 17:23:36,185 [10228] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == false 被调用
2015-01-13 17:23:37,730 [5544] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest 被调用
2015-01-13 17:23:37,730 [5544] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule GetPermissionId 被调用
2015-01-13 17:23:37,730 [5544] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - Url 地址是[/HL2008/UI/AutoMonitor/135?autorun=true]
2015-01-13 17:23:37,745 [5544] INFO  Demo.DemoHttpModule [(null)] <(null)>
 - DemoHttpModule context_BeginRequest -GlobalDefine.IsAutoMon == true 被调用

再次部署到生产环境,问题终于解决.
小结如下:

IIS7.5 404,将system.web中的httpModule注册到system.webServer的modules,如果应用程序池是经典模式,module并没有正确的激活.会出现异常终止.
1.只有将应用程序池切换到集成模式,并彻底剔除掉system.web的httpModules和httpHandlers配置节,才能够正确激活module.(如果不移除经典模式下使用的 httpModules 和 httpHandlers 注册,则必须将validation 元素的 validateIntegratedModeConfiguration 属性设置为false 以避免错误)

<pre class="html" name="code"><system.webServer>				   	        
                <modules runAllManagedModulesForAllRequests="true">
                </modules>
		<span style="color:#ff0000;"><validation validateIntegratedModeConfiguration="false"/>
</span>		<handlers>
		</handlers>
  </system.webServer>
 

2.集成模式下,需要将原经典模式下的system.web下的httpHandlers下的内容迁移到system.webServer下.才能够保证httpHandlers(asp.net框架级别)能够正常运行.

ps:感谢****的客服,这篇文章在昨天晚上11点左右发布的,今天下午来修改这篇博文时,发现账号被盗了,且本文也被删除了,是csnd的客服帮我找回了密码,并找回了这篇文章,再次表示感谢!!