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”文件,所以,需要进行应用程序池回收操作才能生效。

  • 相关阅读:
    Web前端之jQuery 的10大操作技巧
    Python开发者须知 —— Bottle框架常见的几个坑
    string、const char*、 char* 、char[]相互转换
    SLAM中的变换(旋转与位移)表示方法
    SLAM
    二叉搜索树(BST)
    Linux下OSG的编译和安装以及遇到的问题
    CMake--Set用法
    CMake--List用法
    php面向对象面试题
  • 原文地址:https://www.cnblogs.com/cjnmy36723/p/4468872.html
Copyright © 2011-2022 走看看