/// <summary> /// Gets the version of IIS corresponding with the current OS. /// </summary> private void FindIISVersion() { if (iisVersion == IISVersion.None) { Version osVersion = Environment.OSVersion.Version; if (osVersion.Major < 5) { // Win NT 4 kernel -> IIS4 iisVersion = IISVersion.Four; } else { switch (osVersion.Minor) { case 0: // Win 2000 kernel -> IIS5 iisVersion = IISVersion.Five; break; case 1: // Win XP kernel -> IIS5 iisVersion = IISVersion.Five; break; case 2: // Win 2003 kernel -> IIS6 iisVersion = IISVersion.Six; break; } } } }
/// <summary> /// Constructs a new instance of the <see cref="VirtualDirectoryHelper"/> class. /// </summary> /// <param name="serverName">The server to connect to.</param> public VirtualDirectoryHelper(string serverName) { if(serverName == String.Empty) { throw new ArgumentNullException("serverName"); } serverName = serverName; iisVersion = IISVersion.None; FindIISVersion(); }
/// <summary> /// Constructs a new instance of the <see cref="VirtualDirectoryHelper"/> class. /// </summary> /// <param name="serverName">The server to connect to.</param> public VirtualDirectoryHelper(string serverName) { if (serverName == String.Empty) { throw new ArgumentNullException("serverName"); } serverName = serverName; iisVersion = IISVersion.None; FindIISVersion(); }
public static IIIS Create() { IISVersion iisVersion = GetIISVersion(); if (iisVersion == IISVersion.IIS7) { return(new IIS7()); } return(null); }
/// <summary> /// Gets the IIS version. /// </summary> /// <returns>The <see cref="IISVersion"/> for IIS.</returns> /// <exclude/> protected IISVersion GetIISVersion() { System.Version osVersion; if (mServerName == "localhost") { osVersion = Environment.OSVersion.Version; } else { // Make call to remote machine for OS version osVersion = GetRemoteOSVersion(); } IISVersion iisVersion = IISVersion.Six; if (osVersion.Major < 5) { // Windows NT: IIS 4 iisVersion = IISVersion.Four; } else { switch (osVersion.Minor) { case 0: case 1: // Windows 2000 or Windows XP: IIS 5 iisVersion = IISVersion.Five; break; case 2: // Windows Server 2003: IIS 6 iisVersion = IISVersion.Six; break; } } return(iisVersion); }
public static IISOperator GetIISOperator(IISVersion iisVersion) { IISOperator iisOperator; switch (iisVersion) { case IISVersion.IIS5: case IISVersion.IIS6: iisOperator = new IISOperator_V6(); break; case IISVersion.IIS7: case IISVersion.IIS8: iisOperator = new IISOperator_V7(); break; default: throw new IISException("不支持当前的IIS版本。", iisVersion); } iisOperator._iisVersion = iisVersion; return(iisOperator); }
/// <summary> /// Constructs a new instance of the <see cref="VirtualDirectoryHelper"/> class. /// </summary> public VirtualDirectoryHelper() { serverName = "localhost"; iisVersion = IISVersion.None; FindIISVersion(); }
public IISException(IISVersion iisVersion) : base() { _iisVersion = iisVersion; }
public IISException(string message, Exception innerException, IISVersion iisVersion) : base(message, innerException) { _iisVersion = iisVersion; }
public IISException(string message, IISVersion iisVersion) : base(message) { _iisVersion = iisVersion; }
/// <summary> /// Gets the IIS version. /// </summary> /// <returns>The <see cref="IISVersion"/> for IIS.</returns> /// <exclude/> protected IISVersion GetIISVersion() { System.Version osVersion; if (mServerName == "localhost") { osVersion = Environment.OSVersion.Version; } else { // Make call to remote machine for OS version osVersion = GetRemoteOSVersion(); } IISVersion iisVersion = IISVersion.Six; if (osVersion.Major < 5) { // Windows NT: IIS 4 iisVersion = IISVersion.Four; } else if (osVersion.Major == 5) { switch (osVersion.Minor) { case 0: case 1: // Windows 2000 or Windows XP: IIS 5 iisVersion = IISVersion.Five; break; case 2: // Windows Server 2003: IIS 6 iisVersion = IISVersion.Six; break; } } else if (osVersion.Major == 6) { switch (osVersion.Minor) { case 0: // Windows Vista and Windows Server 2008 iisVersion = IISVersion.Seven; break; case 1: // Windows 7 and Windows 2008 R2 iisVersion = IISVersion.SevenFive; break; case 2: // Windows 8 and Windows Server 2012 iisVersion = IISVersion.Eight; break; case 3: // Windows 8.1 and Windows Server 2012 R2 iisVersion = IISVersion.EightFive; break; } } else if (osVersion.Major == 10) { switch (osVersion.Minor) { case 0: // Windows 10.0, Windows Server 2016 and Windows Server 2019 iisVersion = IISVersion.Ten; break; } } return(iisVersion); }
//自动配置WebConfig public static string[] SetWebConfig(IISVersion version, string appRoot, string AppName) { Config.appRoot = appRoot; Config.AssemblyTitle = new DirectoryInfo(Config.appRoot).Name; //文档是否变动 bool changed = false; //配置.net信息 string webConfig = MapPath("/Web.config"); if (!File.Exists(webConfig)) { changed = true; File.Copy(MapPath("/NFinal/Template/Web.config.tpl"), MapPath("/Web.config")); File.Copy(MapPath("/NFinal/Template/Web.config.Debug.tpl"), MapPath("/Web.Debug.config")); File.Copy(MapPath("/NFinal/Template/Web.config.Release.tpl"), MapPath("/Web.Release.config")); } //根据环境配置webconfig System.Xml.XmlDocument doc = new XmlDocument(); doc.Load(webConfig); XmlNode root = doc.DocumentElement; if (root == null) { return(null); } //首选注释掉ConnectionString连接,以免连接字符串不正确而导致生成失败 XmlNode connsNode = doc.DocumentElement.SelectSingleNode("connectionStrings/add[@name='Common']"); XmlNode connsNodeRoot = doc.DocumentElement.SelectSingleNode("connectionStrings"); //如果通用数据库没有添加,则注释掉原有的连接,建立通用连接 if (connsNode == null) { changed = true; if (connsNodeRoot == null) { connsNodeRoot = doc.CreateElement("connectionStrings"); doc.DocumentElement.AppendChild(connsNodeRoot); } connsNodeRoot.InnerXml = "<!--" + connsNodeRoot.InnerXml + "-->" + "<add name=\"Common\" connectionString=\"Data Source=|DataDirectory|\\Common.db;Pooling=true;FailIfMissing=false\" providerName=\"System.Data.SQLite\"/>"; } //读取Apps中的值 if (string.IsNullOrEmpty(AppName)) { AppName = "App"; } XmlNode appsNode = doc.DocumentElement.SelectSingleNode("appSettings/add[@key='Apps']"); string[] Apps = null; List <string> AppsList = new List <string>(); if (appsNode != null && appsNode.Attributes.Count > 0 && appsNode.Attributes["value"] != null) { if (appsNode.Attributes["value"].Value.Trim() != string.Empty) { Apps = appsNode.Attributes["value"].Value.Split(','); for (int i = 0; i < Apps.Length; i++) { if (Directory.Exists(MapPath("/" + Apps[i]))) { AppsList.Add(Apps[i]); } } bool hasAppName = false; for (int i = 0; i < Apps.Length; i++) { if (AppName == Apps[i]) { hasAppName = true; } } if (!hasAppName) { changed = true; AppsList.Add(AppName); Apps = AppsList.ToArray(); appsNode.Attributes["value"].Value = String.Join(",", Apps); } } else { changed = true; Apps = new string[] { AppName }; appsNode.Attributes["value"].Value = AppName; AppsList.Add(AppName); Apps = AppsList.ToArray(); appsNode.Attributes["value"].Value = String.Join(",", Apps); } } else { changed = true; XmlNode appSettingNode = CreateXMLPath(doc, "configuration/appSettings"); appsNode = doc.CreateElement("add"); XmlAttribute attrKey = doc.CreateAttribute("key"); attrKey.Value = "Apps"; XmlAttribute attrValue = doc.CreateAttribute("value"); attrValue.Value = AppName; appsNode.Attributes.RemoveAll(); appsNode.Attributes.Append(attrKey); appsNode.Attributes.Append(attrValue); appSettingNode.AppendChild(appsNode); Apps = new string[] { AppName }; AppsList.Add(AppName); Apps = AppsList.ToArray(); appsNode.Attributes["value"].Value = String.Join(",", Apps); } //读取是否自动生成 XmlNode autoGenerationNode = doc.DocumentElement.SelectSingleNode("appSettings/add[@key='AutoGeneration']"); bool autoGeneration = false; if (autoGenerationNode != null) { autoGeneration = autoGenerationNode.Attributes["value"].Value.ToLower() == "true"; } else { changed = true; XmlNode appSettingNode = CreateXMLPath(doc, "configuration/appSettings"); appsNode = doc.CreateElement("add"); XmlAttribute attrKey = doc.CreateAttribute("key"); attrKey.Value = "AutoGeneration"; XmlAttribute attrValue = doc.CreateAttribute("value"); attrValue.Value = "false"; appsNode.Attributes.RemoveAll(); appsNode.Attributes.Append(attrKey); appsNode.Attributes.Append(attrValue); appSettingNode.AppendChild(appsNode); autoGeneration = false; } Config.autoGeneration = autoGeneration; SetAutoCreateConfig(autoGeneration); //创建URL重写的主页文件 if (!File.Exists(MapPath("/Index.html"))) { VTemplate.Engine.TemplateDocument docIndex = new VTemplate.Engine.TemplateDocument(MapPath("/NFinal/Template/Index.html"), System.Text.Encoding.UTF8); docIndex.SetValue("App", Apps[0]); docIndex.RenderTo(MapPath("/Index.html")); } //删除UrlRewriter string urlRewriterFileName = MapPath("/bin/URLRewriter.dll"); if (File.Exists(urlRewriterFileName)) { File.Delete(urlRewriterFileName); } //添加UrlRewriter XmlNode nodeConfigSection = doc.DocumentElement.SelectSingleNode("configSections/section[@name='RewriterConfig']"); if (nodeConfigSection == null) { changed = true; XmlNode nodeSection = doc.CreateElement("section"); XmlAttribute attrName = doc.CreateAttribute("name"); attrName.Value = "RewriterConfig"; XmlAttribute attrType = doc.CreateAttribute("type"); attrType.Value = "NFinal.URLRewriter.Config.RewriterConfigSerializerSectionHandler"; nodeSection.Attributes.Append(attrName); nodeSection.Attributes.Append(attrType); XmlNode nodeConfigSections = doc.DocumentElement.SelectSingleNode("configSections"); if (nodeConfigSections == null) { nodeConfigSections = doc.CreateElement("configSections"); doc.DocumentElement.InsertBefore(nodeConfigSections, doc.DocumentElement.FirstChild); } nodeConfigSections.AppendChild(nodeSection); } XmlNode nodeRewriteSection = doc.DocumentElement.SelectSingleNode("configSections/section[@name='RewriterConfig']"); //修正属性 if (nodeRewriteSection.Attributes["type"].Value.IndexOf(",") > -1) { changed = true; nodeRewriteSection.Attributes["type"].Value = "NFinal.URLRewriter.Config.RewriterConfigSerializerSectionHandler"; } XmlNode nodeRewriterConfig = doc.DocumentElement.SelectSingleNode("RewriterConfig"); if (nodeRewriterConfig == null) { changed = true; XmlNode nodeRules = CreateXMLPath(doc, "configuration/RewriterConfig/Rules"); XmlNode nodeRewriterRule = doc.CreateElement("RewriterRule"); XmlNode nodeLookFor = doc.CreateElement("LookFor"); nodeLookFor.InnerText = "~/Index.html"; XmlNode nodeSendTo = doc.CreateElement("SendTo"); nodeSendTo.InnerText = string.Format("~/{0}/IndexController/Index.htm", AppName); nodeRewriterRule.AppendChild(nodeLookFor); nodeRewriterRule.AppendChild(nodeSendTo); nodeRules.AppendChild(nodeRewriterRule); } else { XmlNodeList nodeRewriterRules = doc.DocumentElement.SelectNodes("RewriterConfig/Rules/RewriterRule"); if (nodeRewriterRules != null) { for (int i = 0; i < nodeRewriterRules.Count; i++) { XmlNode nodeLookFor = nodeRewriterRules[i].SelectSingleNode("LookFor"); if (nodeLookFor != null && nodeLookFor.InnerText == "~/Index.html") { XmlNode nodeSendTo = nodeRewriterRules[i].SelectSingleNode("SendTo"); if (nodeSendTo != null) { changed = true; nodeSendTo.InnerText = string.Format("~/{0}/IndexController/Index.htm", AppName); } } } } } //如果是IIS7,IIS8新版本,要添加system.webserver配置节 if (version == IISVersion.IIS7 || version == IISVersion.IIS8) { //取消验证 XmlNode newValidationNode = doc.DocumentElement.SelectSingleNode("system.webServer/validation"); if (newValidationNode == null) { newValidationNode = CreateXMLPath(doc, "configuration/system.webServer/validation"); } newValidationNode = doc.DocumentElement.SelectSingleNode("system.webServer/validation"); if (newValidationNode.Attributes["validateIntegratedModeConfiguration"] != null) { newValidationNode.Attributes["validateIntegratedModeConfiguration"].Value = "false"; } else { XmlAttribute validateIntegratedModeConfigurationAttr = doc.CreateAttribute("validateIntegratedModeConfiguration"); validateIntegratedModeConfigurationAttr.Value = "false"; newValidationNode.Attributes.Append(validateIntegratedModeConfigurationAttr); } //添加NFinal节点属性 XmlNode newFactoryNode = doc.DocumentElement.SelectSingleNode("system.webServer/handlers/add[@type='NFinal.Handler.HandlerFactory']"); //不存在则添加 if (newFactoryNode == null) { changed = true; XmlNode handlersNode = CreateXMLPath(doc, "configuration/system.webServer/handlers"); XmlNode NFinalHandlerNode = doc.CreateElement("add"); XmlAttribute attrName = doc.CreateAttribute("name"); attrName.Value = "NFinalHandlerFactory"; XmlAttribute attrVerb = doc.CreateAttribute("verb"); attrVerb.Value = "*"; XmlAttribute attrPath = doc.CreateAttribute("path"); attrPath.Value = "*.htm"; XmlAttribute attrType = doc.CreateAttribute("type"); attrType.Value = "NFinal.Handler.HandlerFactory"; XmlAttribute attrPreCondition = doc.CreateAttribute("preCondition"); attrPreCondition.Value = "integratedMode"; NFinalHandlerNode.Attributes.RemoveAll(); NFinalHandlerNode.Attributes.Append(attrName); NFinalHandlerNode.Attributes.Append(attrVerb); NFinalHandlerNode.Attributes.Append(attrPath); NFinalHandlerNode.Attributes.Append(attrType); NFinalHandlerNode.Attributes.Append(attrPreCondition); handlersNode.AppendChild(NFinalHandlerNode); } //添加Rewriter节点属性 XmlNode newRewriterNode = doc.DocumentElement.SelectSingleNode("system.webServer/modules/add[@name='ModuleRewriter']"); if (newRewriterNode == null) { changed = true; XmlNode modulesNode = CreateXMLPath(doc, "configuration/system.webServer/modules"); XmlNode urlRewriterNode = doc.CreateElement("add"); XmlAttribute attrName = doc.CreateAttribute("name"); attrName.Value = "ModuleRewriter"; XmlAttribute attrType = doc.CreateAttribute("type"); attrType.Value = "NFinal.URLRewriter.ModuleRewriter"; urlRewriterNode.Attributes.Append(attrName); urlRewriterNode.Attributes.Append(attrType); modulesNode.AppendChild(urlRewriterNode); } XmlNode nodeModules = doc.DocumentElement.SelectSingleNode("system.webServer/modules/add[@name='ModuleRewriter']"); if (nodeModules.Attributes["type"] != null) { if (nodeModules.Attributes["type"].Value.IndexOf(",") > -1) { changed = true; nodeModules.Attributes["type"].Value = "NFinal.URLRewriter.ModuleRewriter"; } } } //IIS8下要删除老的配置节点 if (version == IISVersion.IIS8) { XmlNode FactoryNode = doc.DocumentElement.SelectSingleNode("system.web/httpHandlers"); if (FactoryNode != null) { FactoryNode.ParentNode.RemoveChild(FactoryNode); } XmlNode RewriterNode = doc.DocumentElement.SelectSingleNode("system.web/httpModules"); if (RewriterNode != null) { RewriterNode.ParentNode.RemoveChild(RewriterNode); } } //IS6,IIS7下的system.web配置 if (version == IISVersion.IIS6 || version == IISVersion.IIS7) { //添加NFinal节点属性 XmlNode FactoryNode = doc.DocumentElement.SelectSingleNode("system.web/httpHandlers/add[@type='NFinal.Handler.HandlerFactory']"); //不存在则添加 if (FactoryNode == null) { changed = true; XmlNode handlersNode = CreateXMLPath(doc, "configuration/system.web/httpHandlers"); XmlNode NFinalHandlerNode = doc.CreateElement("add"); XmlAttribute attrVerb = doc.CreateAttribute("verb"); attrVerb.Value = "*"; XmlAttribute attrPath = doc.CreateAttribute("path"); attrPath.Value = "*.htm"; XmlAttribute attrType = doc.CreateAttribute("type"); attrType.Value = "NFinal.Handler.HandlerFactory"; NFinalHandlerNode.Attributes.RemoveAll(); NFinalHandlerNode.Attributes.Append(attrVerb); NFinalHandlerNode.Attributes.Append(attrPath); NFinalHandlerNode.Attributes.Append(attrType); handlersNode.AppendChild(NFinalHandlerNode); } //添加Rewriter节点属性 XmlNode RewriterNode = doc.DocumentElement.SelectSingleNode("system.web/httpModules/add[@type='NFinal.URLRewriter.ModuleRewriter']"); if (RewriterNode == null) { changed = true; XmlNode modulesNode = CreateXMLPath(doc, "configuration/system.web/httpModules"); XmlNode urlRewriterNode = doc.CreateElement("add"); XmlAttribute attrName = doc.CreateAttribute("name"); attrName.Value = "ModuleRewriter"; XmlAttribute attrType = doc.CreateAttribute("type"); attrType.Value = "NFinal.URLRewriter.ModuleRewriter"; urlRewriterNode.Attributes.Append(attrName); urlRewriterNode.Attributes.Append(attrType); modulesNode.AppendChild(urlRewriterNode); } //防止使用Rewriter的项目已经使用了别的URL重写模块 XmlNode rewriterModuleNode = doc.DocumentElement.SelectSingleNode("system.web/httpModules/add[@name='ModuleRewriter']"); if (rewriterModuleNode.Attributes["type"] != null) { if (rewriterModuleNode.Attributes["type"].Value.IndexOf(",") > -1) { changed = true; rewriterModuleNode.Attributes["type"].Value = "NFinal.URLRewriter.ModuleRewriter"; } } } //IIS6下要删除新的system.webServer的配置 if (version == IISVersion.IIS6) { XmlNode newWebServerNode = doc.DocumentElement.SelectSingleNode("system.webServer"); if (newWebServerNode != null) { newWebServerNode.ParentNode.RemoveChild(newWebServerNode); } } //如果文档变动就保存 if (changed) { doc.Save(webConfig); } return(Apps); }