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