如何为使用C#的IIS网站添加默认错误页面?
问题描述:
我知道如何在Web.config文件如何为使用C#的IIS网站添加默认错误页面?
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
使用asp.net添加错误页IIS网站,我们可以在IIS中手动添加。
但我想添加基于网站名称的错误页面。
对于举例..
网站名称: “Foo1”
错误页面应该是:\ Foo1 \ err.html
如何从C#中使用控制台添加错误页面或WinForms。 请帮助我
答
您可以在您的网站中修改您的web.config文件,假设它正在运行的应用程序池具有修改它的正确权限。
这可以使用WebConfigurationManager类来完成。
假设你只是想修改的defaultRedirect,你应该能够使用如下代码如下:
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");
if(section != null)
{
section.DefaultRedirect = "yourpage.htm";
configuration.Save();
}
编辑:如果你是想通过Microsoft.Web.Administration要做到这一点,那么下面的代码应该允许您访问特定网站的网页配置,并将customErrors defaultRedirect设置为新值:
using (ServerManager serverManager = new ServerManager())
{
Configuration configuration = serverManager.GetWebConfiguration("your website name");
ConfigurationSection customErrorsSection = configuration.GetSection("system.web/customErrors");
customErrorsSection.SetAttributeValue("defaultRedirect", "/your error page.htm");
serverManager.CommitChanges();
}
“使用控制台或WinForms”是什么意思?你想通过代码来改变各个网站的web.config文件吗? – Fabiano
No @Fabiano ,.我想添加使用System.Web.Administation DLL的错误页面。不是通过使用web.config – Prashee