zoukankan      html  css  js  c++  java
  • C# 使用代码来操作 IIS

    由于需要维护网站的时候,可以自动将所有的站点HTTP重定向到指定的静态页面上。

    要操作 IIS 主要使用到的是“Microsoft.Web.Administration.dll”。
    该类库不可以在引用里找到,存放在“C:WindowsSystem32inetsrv”目录下。
     
    Microsoft.Web.Administration.ServerManager

    该类是操作 IIS 的类。

                var siteName = "admin.rapid.com";
    
                using (ServerManager serverManager = new ServerManager())
                {
                    //获得 IIS 站点信息。
                    var site = serverManager.Sites[siteName];
    
                    //获得站点根目录下的“Web.Config”文件配置信息。
                    var config = site.GetWebConfiguration();
    
                    //取得站点根目录下的“Web.Config”文件 => “<system.webServer>”节点下的“<httpRedirect>”节点,如果不存在,则创建。
                    ConfigurationSection httpRedirectSection = config.GetSection("system.webServer/httpRedirect");
    
                    /*
                     * 设置节点参数。
                     * enabled:是否启用。
                     * destination:目标 URL 或者文件。
                     * exactDestination:
                     * httpResponseStatus:
                     */
                    httpRedirectSection["enabled"] = false;
                    httpRedirectSection["destination"] = @"http://www.rapid.com/error/500$S$Q";
                    httpRedirectSection["exactDestination"] = true;
                    httpRedirectSection["httpResponseStatus"] = @"Temporary";
    
                    //回收应用程序池。
                    serverManager.ApplicationPools[siteName].Recycle();
    
                    //提交。
                    serverManager.CommitChanges();
                }

    注释已说明。

    由于提交后,会直接修改站点根目录 下的“Web.config”文件,所以,需要进行应用程序池回收操作才能生效。

  • 相关阅读:
    精选30道Java笔试题解答
    ASM
    Java Decompiler Plugin For Eclipse IDE
    AMQ5540, AMQ5541 and AMQ5542, application did not supply a user ID and password, 2035 MQRC_NOT_AUTHORIZED
    Shell脚本中的export
    Linux set unset命令
    shell中${}的妙用
    ubuntu alsa2
    ubuntu alsa
    计算机启动boot
  • 原文地址:https://www.cnblogs.com/cjnmy36723/p/4468872.html
Copyright © 2011-2022 走看看