C# 如何重定向到预定义的错误页面?
我的web.config中有错误页面设置,如:C# 如何重定向到预定义的错误页面?,c#,asp.net,redirect,asp.net-4.0,C#,Asp.net,Redirect,Asp.net 4.0,我的web.config中有错误页面设置,如: <customErrors mode="RemoteOnly" defaultRedirect="ErrorDocs/500.htm"> <error statusCode="404" redirect="ErrorDocs/404.htm"/> <error statusCode="403" redirect="ErrorDocs/403.htm"/> </customErrors>
<customErrors mode="RemoteOnly" defaultRedirect="ErrorDocs/500.htm">
<error statusCode="404" redirect="ErrorDocs/404.htm"/>
<error statusCode="403" redirect="ErrorDocs/403.htm"/>
</customErrors>
有没有一种简单的方法可以在不输入名称的情况下重定向到404页面?例如:响应重定向(404状态编码页);
或者有没有办法获取默认的404位置?没有,很遗憾,这些路径只适用于静态页面路径。没有,很遗憾,这些路径只适用于静态页面路径。从这里的答案: 答案如下: 如果愿意,您当然可以通过编程方式从web.config文件中获取设置--大致代码:
string my404Setting;
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
foreach (ConfigurationSectionGroup sectionGroup in config.SectionGroups)
{
if (sectionGroup.type == "customErrors" )
{
foreach (ConfigurationSections section in sectionGroup.Sections)
{
if (section.StatusCode = "404")
{
my404Setting = section.Redirect;
break;
}
}
break;
}
}
}
比应该的更难看,但这就是你阅读所需内容的方式。如果你愿意,你当然可以通过编程方式从web.config文件中获取设置--大致代码:
string my404Setting;
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
foreach (ConfigurationSectionGroup sectionGroup in config.SectionGroups)
{
if (sectionGroup.type == "customErrors" )
{
foreach (ConfigurationSections section in sectionGroup.Sections)
{
if (section.StatusCode = "404")
{
my404Setting = section.Redirect;
break;
}
}
break;
}
}
}
比应该的更难看,但这就是你阅读所需内容的方式。虽然我已经实现了另一种处理错误的方法,但这是最接近的答案。虽然我已经实现了另一种处理错误的方法,但这是最接近的答案。