web.config没有转发到非.aspx页面上的404错误页面

JGa*_*rdo 12 asp.net redirect web-config

目的

我希望所有丢失页面的URL都转发到我的根页中的404页面 404error.aspx

问题

到目前为止,只有具有.aspx遗嘱的网址才有效.例如,如果输入,4error.aspx您将被重定向到错误页面.

/404error.aspx?aspxerrorpath=/4error.aspx
Run Code Online (Sandbox Code Playgroud)

背景

我不是.NET开发人员,所以我接手了这个使用经典的项目.aspx.因此,我没有使用任何Microsoft产品在模板或框架中构建任何此类产品.我只是在Sublime文本编码.

我已经研究过的

我查看了许多我在Google上发现的文章,但没有任何实际工作的完整示例.

这是我开始使用的完整代码 web.config

web.config中

<configuration>
  <system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
      <globalization
        fileEncoding="utf-8"
        requestEncoding="utf-8"
        responseEncoding="utf-8"
        culture="en-US"
        uiCulture="de-DE"
      />
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我也从微软发现了这个例子(404error.aspx是我的修改版)
http://msdn.microsoft.com/en-us/library/vstudio/bb397417%28v=vs.100%29.aspx

web.config(2)

<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <compilation debug="true" />

    <!-- Turn on Custom Errors -->
    <customErrors mode="On" 
      defaultRedirect="/404error.aspx">
      <error statusCode="404" redirect="/404Error.aspx"/>
    </customErrors>

  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

这也没有处理没有的页面 .aspx

然后我尝试了这个例子

web.config(3)

<?xml version="1.0"?>
<configuration>
   <system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
    <error statusCode="404" redirect="~/404error.aspx" />
    <globalization
      fileEncoding="utf-8"
      requestEncoding="utf-8"
      responseEncoding="utf-8"
      culture="en-US"
      uiCulture="de-DE"
    />
   </system.web>
   <system.webServer>
      <httpErrors>
        <remove statusCode="401" subStatusCode="-1" />
        <remove statusCode="403" subStatusCode="-1" />      
        <remove statusCode="404" subStatusCode="-1" />                
        <remove statusCode="500" subStatusCode="-1" />
          <!-- full url when responsemode is Redirect -->
        <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
          <!-- local relative path when responsemode is ExecuteURL -->
        <error statusCode="403" path="~/404error.aspx" responseMode="ExecuteURL" />
        <error statusCode="404" path="~/404error.aspx" responseMode="ExecuteURL" />                
        <error statusCode="500" path="~/404error.aspx" responseMode="ExecuteURL" />
      </httpErrors>
      <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

显然我必须更改404以外的其他页面的路径,但我只想测试它们的错误.那最后的web.config工作更糟糕.

你能告诉我需要修改的内容吗?我会很感激,<configuration>因为我一直很困惑.

编辑1

我读过许多开发人员建议只是把它变成一个HTML页面.所以现在我的页面404.html是我的更新web.config

<configuration>
  <system.web>
    <customErrors mode="On"
              redirectMode="ResponseRewrite">
        <error statusCode="404"
           redirect="~/404.html"/>
      </customErrors>
      <globalization
        fileEncoding="utf-8"
        requestEncoding="utf-8"
        responseEncoding="utf-8"
        culture="en-US"
        uiCulture="de-DE"
      />
  </system.web>

  <system.webServer>
    <httpErrors errorMode="Custom"
            defaultResponseMode="File">
      <remove statusCode="404"/>
      <error statusCode="404"
          path="~/404.html"/>
    </httpErrors>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Kei*_*ith 8

您需要配置<httpErrors>元素.这将配置静态文件和服务器页面的错误页面.

你的第三次尝试"web.config(3)"和"编辑1" 几乎就在那里.问题是你不能在这里使用app-relative路径(例如:"〜/ 404.html"),它们必须是站点根目录(例如:"/ 404.html").

<?xml version="1.0"?>
<configuration>
   <system.webServer>
      <httpErrors>
        <remove statusCode="401" subStatusCode="-1" />
        <remove statusCode="403" subStatusCode="-1" />      
        <remove statusCode="404" subStatusCode="-1" />                
        <remove statusCode="500" subStatusCode="-1" />
          <!-- full url when responsemode is Redirect -->
        <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
          <!-- local relative path when responsemode is ExecuteURL -->
        <error statusCode="403" path="/404error.aspx" responseMode="ExecuteURL" />
        <error statusCode="404" path="/404error.aspx" responseMode="ExecuteURL" />                
        <error statusCode="500" path="/404error.aspx" responseMode="ExecuteURL" />
      </httpErrors>
   </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)