以编程方式设置自定义404错误页面

时间:2022-10-09 21:07:52

In a regular ASP.NET application, you might have a customErrors section like the following:

在常规ASP.NET应用程序中,您可能具有如下所示的customErrors部分:

<customErrors mode="On">
  <error statusCode="404" redirect="Nice-FileNotFound-Page.aspx"/>
</customErrors> 

Is it possible to set the 404 error programmatically at Application_Start? Something like the following:

是否可以在Application_Start上以编程方式设置404错误?类似于以下内容:

// Figure out which page should be used... using some logic somewhere in the application
// (obviously) Pseudo-code: Application.ErrorCode.404.Page = Page-Found-Above

2 个解决方案

#1


You can put that logic in Nice-FileNotFound-Page.aspx and redirect from there to the appropriate page using any business logic you need.

您可以将该逻辑放在Nice-FileNotFound-Page.aspx中,并使用您需要的任何业务逻辑从那里重定向到相应的页面。

#2


This is not asp specific, but as long as "Nice-FileNotFound-Page.aspx" is a dynamic script, couldnt you just make is be whatever you want it to be? That is to say, let it figure out programatically what kind of page it is supposed to be based on the logic and then be that page?

这不是特定于asp的,但只要“Nice-FileNotFound-Page.aspx”是一个动态脚本,你不能只是做出你想要的任何东西吗?也就是说,让它以编程方式弄清楚它应该是基于逻辑的那种页面然后是那个页面?

#1


You can put that logic in Nice-FileNotFound-Page.aspx and redirect from there to the appropriate page using any business logic you need.

您可以将该逻辑放在Nice-FileNotFound-Page.aspx中,并使用您需要的任何业务逻辑从那里重定向到相应的页面。

#2


This is not asp specific, but as long as "Nice-FileNotFound-Page.aspx" is a dynamic script, couldnt you just make is be whatever you want it to be? That is to say, let it figure out programatically what kind of page it is supposed to be based on the logic and then be that page?

这不是特定于asp的,但只要“Nice-FileNotFound-Page.aspx”是一个动态脚本,你不能只是做出你想要的任何东西吗?也就是说,让它以编程方式弄清楚它应该是基于逻辑的那种页面然后是那个页面?