2009-12-07 47 views
1
工作

我接着对如何设置404忠告:获取所有404对IIS6

http://www.andornot.com/about/developerblog/archive/2009_10_01_archive.aspx

及相关:

Best way to implement a 404 in ASP.NET

从Global.asax中:

protected void Application_Error(Object sender, EventArgs e) 
{ 
    Exception exception = Server.GetLastError(); 
    if (exception is HttpUnhandledException) 
    { 
     if (exception.InnerException == null) 
     { 
      Server.Transfer(string.Format("~/Error.aspx", false)); 
      return; 
     } 
     exception = exception.InnerException; 
    } 

    if (exception is HttpException) 
    { 
     if (((HttpException)exception).GetHttpCode() == 404) 
     { 
      Server.ClearError(); 
      Server.Transfer("~/404.aspx", false); 
      return; 
     } 
    } 

    if (Context != null && Context.IsCustomErrorEnabled) 
    { 
     Server.Transfer(string.Format("~/Error.aspx"), false); 
    } 

} 

而且从Web.config文件:

<customErrors mode="On"/> 

它在测试(VS2010)时在本地很精美,但在生产(ISS6)中它只能用于aspx页面。 http://mysite.se/foo.js让我看看ISS 404页面。 (“该页无法找到”)

我缺少什么?

回答

4

如果你不想设置通配符映射,或有ASP.NET处理所有静态文件(一般表现很可能会说,你不这样做),你需要在IIS 6配置为发送404对的aspx处理错误的页面。

  1. 打开IIS管理器,浏览到相关站点,右键单击它并选择属性。
  2. 打开“自定义错误”选项卡。
  3. 向下滚动到404,选择它,然后按“编辑...”。
  4. 将消息类型更改为URL,并将其指向存在于服务器(即“/404.aspx”)上的文件

点4是关键 - 它需要指向一个存在的文件,否则IIS将恢复为默认值。

2

Web.Config中指定的404处理程序只处理由ASP.NET运行时处理的文件,包括JavaScript文件在内的所有其他文件将由您在IIS设置中指定的404页处理。这就是为什么你看到http://mysite.se/foo.js IIS生成的错误消息,而不是在Web.Config的自定义错误部分中指定的错误消息。

你可以,但是,这些映射文件类型的ASPNET_ISAPI.DLL让他们通过您的自定义错误页处理。

  1. 打开IIS管理器
  2. 选择Web站点
  3. 点击右键,在右键菜单中选择 属性
  4. 在扩展列找到 的.aspx,双击和 完整路径复制到ASPNET_ISAPI.DLL。它 应该像 C:\ WINDOWS \ Microsoft.NET \框架\ V1.1.4322 \« ASPNET_ISAPI.DLL
  5. 单击添加,粘贴在可执行文件框中
  6. 在扩展名框中键入html的路径
  7. 确保选中检查文件是否存在被 未选中
  8. 关闭所有对话框

here以获取更多信息。