您可以在您的网站中修改您的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