无法读取web.config文件

时间:2017-08-16 11:16:43

标签: c# asp.net-mvc iis web-config

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

 ConfigurationManager.ConnectionStrings[connectionStringName];

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

最后,这是我在IIS服务器中的站点结构。请注意,这三个子网站位于主网站下。 enter image description here

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

以下是完整的跟踪日志:

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

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

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

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

这样做:

打开%userprofile%\My Documents\IISExpress\config\applicationhost.config,在<sites>部分中,检查相应<virtualDirectory>的{​​{1}}节点。您可以从位于<site> <bindings>/<binding>属性中的端口号轻松找到它。

<强> E.g。 :

bindingInformation

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

相关问题