无法读取 web.config 文件

Can not read web.config file

当我们尝试读取配置文件中的连接字符串时出现错误。这是我读取配置文件的方式:

 ConfigurationManager.ConnectionStrings[connectionStringName];

错误如下。 虚拟路径“/site1”映射到另一个应用程序,这是不允许的。

最后,这是我在 IIS 服务器中的站点结构。请注意,三个子网站托管在主网站下。

注意:此错误并非总是会发生。有时所有网站都可以正常工作,没有任何问题。此外,三个子网站引用相同的代码库。

下面是完整的跟踪日志:

     The following exception was thrown: The virtual path '/site1' maps to another application, which is not allowed.. Exception details: System.ArgumentException: The virtual path '/site1' maps to another application, which is not allowed. 
   at System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) 
   at System.Web.HttpContext.GetFilePathData() 
   at System.Web.HttpContext.GetSection(String sectionName) 
   at System.Configuration.ConfigurationManager.GetSection(String sectionName) 
   at System.Configuration.ConfigurationManager.get_ConnectionStrings() 

我好像在以下地方也有这个问题:

   The following exception was thrown: The virtual path '/site1' maps to another application, which is not allowed.. Exception details: System.ArgumentException: The virtual path '/site1' maps to another application, which is not allowed. 
   at System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) 
   at System.Web.HttpContext.GetFilePathData() 
   at System.Web.HttpContext.GetSection(String sectionName) 
   at System.ServiceModel.Activation.HostedAspNetEnvironment.UnsafeGetSectionFromWebConfigurationManager(String sectionPath, String virtualPath) 
   at System.ServiceModel.Configuration.ConfigurationHelpers.UnsafeGetSectionNoTrace(String sectionPath) 
   at System.ServiceModel.Diagnostics.TraceUtility.SetEtwProviderId() 
   at System.ServiceModel.ChannelFactory..ctor() 
   at System.ServiceModel.ChannelFactory`1..ctor(Type channelType) 
   at System.ServiceModel.ChannelFactory`1..ctor(Binding binding, EndpointAddress remoteAddress) 
   at System.ServiceModel.ClientBase`1..ctor(Binding binding, EndpointAddress remoteAddress) 

这似乎是在系统尝试读取 Web 服务设置时发生的。

当 ASP.NET 网站被复制到新文件夹时,解决方案的 "Virtual Path" 属性 通常设置为文件夹名称而不是根目录。将虚拟路径设置更改为“/”以解决此问题。 这可以通过右键单击项目,打开属性对话框来完成:Solution->Properties->Virtual Path-> Change to "/"

尝试 运行 每个站点都在其自己的应用程序池下,并为每个站点(包括主站点)使用单独的 bin 目录。 IIS 可能认为它正在为 Main 服务,但在内存中的某个地方它切换到了 Site1。如有必要,使用 WCF 在站点之间进行通信。

如果 "IIS_IUsers" 组无权编辑您尝试访问其配置的当前应用程序根文件夹中的文件,就会发生这种情况。

根据问题中给出的代码行,这应该默认为当前应用程序的配置。 检查 IIS 是否在允许其访问文件系统的帐户下 运行(类似于 localsystem 或 networkservice 应该这样做)然后在 IIS 中检查应用程序池的帐户,相关应用程序在该帐户下 运行 是 运行 在具有相同权限的帐户下或在 "applicationpoolidentity" 下使用 IIS 的权限。

作为临时测试,您可以授予 "everyone" 对相关目录的完全控制权,然后删除该权限并添加其他权限,直到您满意它只处理您需要的内容。

您的 applicationhost.config 文件中有错误。

您必须检查每个 <application> path 属性对于 <site> 是唯一的!

这样做:

打开 %userprofile%\My Documents\IISExpress\config\applicationhost.config 并在 <sites> 部分检查适当 <site><virtualDirectory> 节点。您可以从 <bindings>/<binding> bindingInformation 属性中的端口号轻松找到它。

例如:

<site name="MyWebSite" id="1234">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="c:\inetpub" />
    </application>
    <application path="/subsite1" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Solution\subsite1" />
    </application>
    <application path="/subsite2" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Solution\subsite2" />
    </application>
    <application path="/subsite3" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Solution\subsite3" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:1234:localhost" />
    </bindings>
</site>  

如您在此示例中所见,每个应用程序路径都是唯一的。