如何更改Asp.Net中customErrors页面的默认行为

Gib*_*boK 2 html c# asp.net http-status-code-404

我使用Asp.Net 4和C#.

我想知道如何更改Asp.Net的默认beahviour customErrors.

在我的Web.Config文件中,我使用此设置.

    <customErrors mode="On" defaultRedirect="/ErrorPages/Oops.aspx">
        <error statusCode="404" redirect="/ErrorPages/404.aspx" />
    </customErrors>
Run Code Online (Sandbox Code Playgroud)

正如您所期望的那样,如果找不到页面,则会在Web.Config中指定的自定义页面中进行重定向.


请求页面:

http://localhost:1372/nopagehere
Run Code Online (Sandbox Code Playgroud)

结果页面:

http://localhost:1372/ErrorPages/404.aspx?aspxerrorpath=/nopagehere
Run Code Online (Sandbox Code Playgroud)

当我分析结果页面的Http Header时,我可以看到:

  • 找不到页面时的404状态
  • 新创建的URL的302状态

我想以这种方式改变这种行为:

知道怎么做吗?谢谢你的时间.

Jan*_*oom 5

我想你正在寻找redirectMode你所在<customErrors>部分的属性.

请参阅:http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx

我想你想把它设置为redirectMode="ResponseRewrite".这会抛出404并显示404页面的内容,但不会重定向到实际的404页面.