无法读取web.config文件

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

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服务设置时,似乎会发生这种情况。

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

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

作为临时测试,您可以将“所有人”完全控制权限授予相关目录,然后删除该权限并添加其他权限,直到您满意为止它只使用您需要的内容。

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

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

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

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

为此:

打开%userprofile%\My Documents\IISExpress\config\applicationhost.config ,在部分中,检查相应节点。 您可以从位于/ bindingInformation属性中的端口号轻松找到它。

例如:

                  

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