Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

노우!쑤

[c#] IIS config 변경 본문

DevOps

[c#] IIS config 변경

no500 2022. 3. 31. 11:42

개요

IIS Rewrite 설정을 동적으로 변경

public void Set()
{
    ServerManager serverManager = new ServerManager(@"%windir%\system32\inetsrv\config\applicationhost.config");
    SiteCollection sites = serverManager.Sites;
    List<Site> proxySites = sites.Where(x => x.Name.Contains("proxy")).ToList();

    Configuration config = proxySites[0].GetWebConfiguration();
    ConfigurationSection rulesSection = config.GetSection("system.webServer/rewrite/rules");
    ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();

    foreach (var r in rulesCollection)
    {
        foreach (var c in r.ChildElements)
        {
            foreach (var a in c.Attributes)
            {
                if (c.ElementTagName == "action")
                {
                    if (a.Name == "url")
                    {
                        a.Value = "http://site1.com/{R:0}";
                    }
                }
            }
        }
    }

    serverManager.CommitChanges();
}