none
VB如何修改IIS某个站点的默认文档、错误页、主机头、端口等等 RRS feed

全部回复

  • 你好,

    如果是IIS7.0 或以上版本,可以通过ServerManager 来做。比如:

    #增加默认文档

    Using localServer As ServerManager = New ServerManager()
          Dim config As Configuration = localServer.GetApplicationHostConfiguration()
          Dim defaultDocumentSection As ConfigurationSection = config.GetSection("system.webServer/defaultDocument", "default")
          Dim files As ConfigurationElement = defaultDocumentSection.GetChildElement("files")
          Dim filesCollection As ConfigurationElementCollection = files.GetCollection()
          Dim newfile As ConfigurationElement = filesCollection.CreateElement()
          newfile.SetAttributeValue("value", "mypage5.html")
          filesCollection.Add(newfile)
          localServer.CommitChanges()
    End Using


    #移除默认文档

    Using localServer As ServerManager = New ServerManager()
                Dim config As Configuration = localServer.GetApplicationHostConfiguration()
                Dim defaultDocumentSection As ConfigurationSection = config.GetSection("system.webServer/defaultDocument", "default")
                Dim files As ConfigurationElement = defaultDocumentSection.GetChildElement("files")
                Dim filesCollection As ConfigurationElementCollection = files.GetCollection()
                Dim newfile As ConfigurationElement = filesCollection.CreateElement()
    
                For Each file As ConfigurationElement In filesCollection
                    If (String.Equals(file.GetAttributeValue("value").ToString(), "mypage2.html", StringComparison.OrdinalIgnoreCase)) Then
                        filesCollection.Remove(file)
                        Exit For
                    End If
                Next
                localServer.CommitChanges()
            End Using

    #Bindinfo 可参考下面的代码

    https://gist.github.com/CharlTruter/2110398

    #错误页面可以参考下面的代码:

    https://stackoverflow.com/questions/44756000/pragmatically-how-to-add-default-error-pages-for-iis-website-using-c

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年1月1日 5:12
    版主