【问题标题】:404 page not working404页面不工作
【发布时间】:2014-01-21 15:46:48
【问题描述】:

当用户请求不存在的页面时,我正在尝试从 Web 配置重定向 404 页面。 This 是我的网络配置。

当我尝试它时,重定向不起作用,我得到的是 IIS 默认 404 页面。我正在本地的 IIS Express 上试用它。

this 是我的页面列表。404 页面位于根目录。

我该怎么做?

【问题讨论】:

  • 请在此处复制/粘贴您的 web.config,不要在图像上显示。不利于查看图片,而且图片可能几天后就不存在了,甚至无法帮助爬虫索引该页面。
  • 要完成这项工作,您必须尝试调用 asp.net 页面,因此从 asp.net 传递并按照您在 web.config 上给他的指示操作。你有尝试吗?或者您提供的页面不是从 asp.net 传递的?看到这个:stackoverflow.com/questions/11045733/…
  • 我已经尝试过您提供的代码但无法正常工作。我在 Web 配置中添加了此代码,但现在该站点正在导航到一个完全空白的页面。如果请求扩展名为 .aspx 的 url,则 404 页面正在工作。我在 webconfig 中添加的代码是

标签: asp.net iis http-status-code-404


【解决方案1】:

IIS 不是很好。 (免责声明)

为了让 IIS 意识到您知道自己在做什么并实际显示您的自定义 404 页面,页面本身必须超过一定的字节大小(确切地说是512 bytes),否则 IIS 假定它知道得更好并且显示它的默认 404。

尝试在 404 页面底部添加注释掉的行以使其“膨胀”一点。听起来很疯狂,我知道,但它确实有效。

编辑:512 bytes 我相信是最小页面大小,否则 IIS 会跳过它。我敢肯定 M$ 的某个人认为这是个好主意。

【讨论】:

  • 谢谢你,我会尝试并告诉结果。
【解决方案2】:

查看您的 web.config 条目让我怀疑您的错误页面 404.aspx 本身是否可能是您看到 404 错误的原因。可能从 redirect="/404.aspx" 中删除 / 就可以了。使用一些开发工具分析您的请求。

【讨论】:

    【解决方案3】:
        <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
    
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <location path="index.aspx">
        <system.webServer>
          <httpRedirect enabled="true" destination="~/Admin/Login.aspx" exactDestination="true" httpResponseStatus="Found" />
          <modules runAllManagedModulesForAllRequests="true"></modules>
        </system.webServer>
      </location>
      <location path="images">
        <system.web>
          <authorization>
            <allow users="*" />
          </authorization>
        </system.web>
      </location>
      <location path="js">
        <system.web>
          <authorization>
            <allow users="*" />
          </authorization>
        </system.web>
      </location>
      <location path="Resimler">
        <system.web>
          <authorization>
            <allow users="*" />
          </authorization>
        </system.web>
      </location>
      <location path="Script">
        <system.web>
          <authorization>
            <allow users="*" />
          </authorization>
        </system.web>
      </location>
      <location path="Scripts">
        <system.web>
          <authorization>
            <allow users="*" />
          </authorization>
        </system.web>
      </location>
      <system.web>
        <customErrors mode="On" defaultRedirect="~/Hata.aspx">
          <error statusCode="404" redirect="~/404.aspx"/>
        </customErrors>
        <pages validateRequest="false">
          <controls>
            <add tagPrefix="CKEditor" assembly="CKEditor.NET" namespace="CKEditor.NET"/>
          </controls>
        </pages>
        <compilation debug="true" targetFramework="4.5">
          <assemblies>
            <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          </assemblies>
        </compilation>
        <authentication mode="Forms">
          <forms name=".ASPXAUTH" loginUrl="Admin/Login.aspx" defaultUrl="Default.aspx" protection="All" timeout="30" path="/" requireSSL="false" slidingExpiration="true" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" ticketCompatibilityMode="Framework20">
            <credentials passwordFormat="SHA1">
              <user name="admin" password="7dc959a7d6440baa02e0bb84b1dffeb173facf5a" />
            </credentials>
          </forms>
          <passport redirectUrl="internal" />
        </authentication>
        <httpRuntime targetFramework="4.5" requestValidationMode="2.0" executionTimeout="1200" maxRequestLength="51200" />
      </system.web>
      <connectionStrings>
        <add name="InsaatEntities" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
        <add name="ModelInsaatEntitiy" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
        <add name="DB120807190835Entities" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
        <add name="ModelInsaatEntities" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
          </parameters>
        </defaultConnectionFactory>
      </entityFramework>
      <appSettings>
        <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
      </appSettings>
    
    </configuration>
    

    这是我的网络配置。当我请求 nonexist.aspx 时,它会正确重定向页面 404.aspx,但是当我请求没有 .aspx 扩展名的 mydomain/nonexists 时,它会重定向默认的 iis 404 页面。我的页面大小是超过 512 字节

    【讨论】:

      【解决方案4】:

      在错误状态代码 404 上设置重定向到“~/404.aspx”这应该可以解决问题。

      如果还是不行,请尝试以下方法:

      <httpErrors errorMode="Custom" existingResponse="Replace">
          <remove statusCode="404" subStatusCode="-1" />
          <error statusCode="404" path="~/404.aspx" responseMode="ExecuteURL" />
      </httpErrors>
      

      将上面的代码放在 web.config 中的 system.webServer 标签内。

      IIS Express 使用 system.webserver,因为它基于 IIS7 代码库: http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx.

      请告诉我它是否有效。

      问候。

      【讨论】:

      • 谢谢你的回复,但是不行。我都试过了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2011-01-08
      相关资源
      最近更新 更多