共同使用相同IP地址及端口号
使用IIS的应用程序功能
这种方法与添加一个新网站的过程形同
在这种情况下,通常会出现将子站继承主站的 Web.config 报错。
解决方法:
首先看第一种方法,即通过修改根目录的Web.config消除继承关系,在根目录Web.config文件的system.web或者一切不想让子目录继承的配置节点外面添加一层location如下:
<configuration> <!--...其他配置...--> <location path="." inheritInChildApplications="false"> <system.web> </system.web> </location> <!--...其他配置...--> </configuration>
或
<location path="." allowOverride="false" inheritInChildApplications="false"> <system.web> <!-- *** --> </system.web> </location>
这里inheritInChildApplications很好理解,即是否允许子目录继承,默认为true,我们修改为false就可以避免继承了。这种方法的优点是很简单,但是不够灵活,同时面对较复杂的Web.config配置,仍可能会报错。比如根目录和子目录有着不同的system.webServer配置,按照这个方法在根目录的system.webServer外面加上了location限制,不幸的是,IIS7下会出现500错误。当然,也有解决方法,可以参考下面这篇文章。
"SOLVED: IIS7, validateIntegratedModeConfiguration and inheritInChildApplications clash"
接着看第二种方法,不用修改根目录的Web.config文件,而是修改子目录的Web.config。假设根目录的Web.config设置了一个名为BlogEngine的连接字符串,要在子目录使用另一个名字为BlogEngine的连接字符串,就需要先清除已有的连接字符串(根目录继承下来的connectionString设置),清除所有的配置,可以用clear语法,清除指定名称的配置,可以用remove语法,如下
<--根目录的Web.config--> <connectionStrings> <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine1; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/> </connectionStrings> <--子目录的Web.config(clear方法)--> <connectionStrings> <clear/> <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine2; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/> </connectionStrings> <--子目录的Web.config(remove方法)--> <connectionStrings> <remove name="BlogEngine"/> <add name="BlogEngine" connectionString="Data Source=localhost\SQLEXPRESS; Initial Catalog=BlogEngine2; User ID=xxx; Password=xxx" providerName="System.Data.SqlClient"/> </connectionStrings>
这里只是用connectionString为例,使用时完全可以应用在所有可以配置的节点上,任何配置节点都可以用clear和remove节点将继承来的配置先清除掉,然后再add新的配置。此方法灵活性更强,同时可以保留根目录Web.config中的部分共同配置(而无需全部重新设定)。下面是一个复杂些的例子,分别是根目录和子目录在system.webServer上的配置。
<--根目录的Web.config system.webServer配置节点--> <modules> <remove name="ScriptModule"/> <add name="WwwSubDomainModule" type="BlogEngine.Core.Web.HttpModules.WwwSubDomainModule, BlogEngine.Core"/> <add name="UrlRewrite" type="BlogEngine.Core.Web.HttpModules.UrlRewrite, BlogEngine.Core"/> <add name="CompressionModule" type="BlogEngine.Core.Web.HttpModules.CompressionModule, BlogEngine.Core"/> <add name="ReferrerModule" type="BlogEngine.Core.Web.HttpModules.ReferrerModule, BlogEngine.Core"/> <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </modules> <--子目录的Web.config system.webServer配置节点--> <modules runAllManagedModulesForAllRequests="true"> <remove name="WwwSubDomainModule"/> <remove name="UrlRewrite"/> <remove name="CompressionModule"/> <remove name="ReferrerModule"/> <remove name="ScriptModule"/> <add name="UrlRewrite" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" /> <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </modules> <handlers>
可以看到,子目录将所有根目录定义的Modules(WwwSubDomainModule,UrlRewrite,CompressionModule,ReferrerModule,ScriptModule)都清除了,添加了自己的UrlRewrite和ScriptModule两个Module。如此配置既消除了冲突,又可以实现了配置的部分继承(子目录只有部分配置和根目录不同),而第一种方法却无法实现部分继承。
还可使用DNS服务器以及网站头等方式来处理