【问题标题】:An error occurred loading a configuration file: Failed to map the path '/'加载配置文件时出错:无法映射路径“/”
【发布时间】:2013-08-29 00:50:57
【问题描述】:

我收到加载配置文件时出错:无法映射路径“/”。当我尝试运行以下行以获取 web.config 的路径时

Configuration config =System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

我正在运行 Windows 7、Visual Studio 2010 和 .net framework 4.0。现在我知道这个问题可以通过以管理员身份运行 Visual Studio 来解决,并且在以管理员身份运行之前我已经多次运行相同的代码,但今天以管理员身份运行也不能解决问题。我在队友的笔记本电脑上运行了相同的代码,并且代码运行良好,但当我在笔记本电脑上运行该代码时,它却无法运行。

我已经尝试了所有方法,有没有一种方法可以提供绝对路径以使其暂时工作,并且我的窗户是否会成为问题,因为它最近状况不佳。请帮忙,因为它浪费了我很多时间

【问题讨论】:

  • 每次我遇到“无法映射路径”时,都是权限问题。这也可以解释为什么它在以管理员身份运行时会起作用(直到今天?)。我会确保你已经读/写了所有的源代码和目录。
  • 很少有文件会锁定它们。我会尝试让它们可写
  • 尝试打开你的 vs 管理员

标签: asp.net .net web-config relative-path


【解决方案1】:

我遇到了同样的问题,最初我使用了以管理员身份启动 Visual Studio 的相同解决方法。

最后我尝试做一些测试并得到了这个:如果你为 web.config 创建一个文件映射,应用程序会在没有管理员权限的情况下加载配置。

我使用了http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.openmappedwebconfiguration(v=vs.110).aspx 中的示例,但做了一些修改:

    public static Configuration GetConfiguration()
    {
        if (HostingEnvironment.ApplicationVirtualPath == "/")
            return WebConfigurationManager.OpenWebConfiguration("~/web.config");

        WebConfigurationFileMap fileMap = CreateFileMap(HostingEnvironment.ApplicationVirtualPath);
        // Get the Configuration object for the mapped virtual directory.
        return WebConfigurationManager.OpenMappedWebConfiguration(fileMap, HostingEnvironment.ApplicationVirtualPath);
    }

    private static WebConfigurationFileMap CreateFileMap(string applicationVirtualPath)
    {

        WebConfigurationFileMap fileMap =
               new WebConfigurationFileMap();

        // Get he physical directory where this app runs. 
        // We'll use it to map the virtual directories 
        // defined next.  
        string physDir = HostingEnvironment.ApplicationPhysicalPath;

        // Create a VirtualDirectoryMapping object to use 
        // as the root directory for the virtual directory 
        // named config.  
        // Note: you must assure that you have a physical subdirectory 
        // named config in the curremt physical directory where this 
        // application runs.
        VirtualDirectoryMapping vDirMap =
            new VirtualDirectoryMapping(physDir, true);

        // Add vDirMap to the VirtualDirectories collection  
        // assigning to it the virtual directory name.
        fileMap.VirtualDirectories.Add(applicationVirtualPath, vDirMap);

        // Create a VirtualDirectoryMapping object to use 
        // as the default directory for all the virtual  
        // directories.
        VirtualDirectoryMapping vDirMapBase =
            new VirtualDirectoryMapping(physDir, true, "web.config");

        // Add it to the virtual directory mapping collection.
        fileMap.VirtualDirectories.Add("/", vDirMapBase);

        // Return the mapping. 
        return fileMap;
    }

我认为这不是一个很酷的修复,但它可以工作并且可以包含在#if DEBUG 部分中。

有了这些,我推断当您启动 Visual Studio Web 开发服务器并将虚拟路径设置设置为“/MyApplication”之类的设置时,如果它已以管理员权限启动,它会创建一些映射,它在没有时不会创建特权。这只是一个我无法验证的假设。

我希望这对其他人有帮助!

【讨论】:

    【解决方案2】:

    试试这个,它一直对我有用:

    Configuration config 
         =System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
    

    甚至是这个:

    Configuration config 
             =System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("./");
    

    确保您引用了System.Configuration.dll 程序集。

    您似乎正在尝试从应用程序打开 Web.Config 配置文件,因此您也可以使用以下一个。这意味着您无论如何都不需要打开文件。检查MSDN here

    var section = WebconfigurationManager.GetSection("Section_Name");
    

    【讨论】:

      【解决方案3】:

      要获取配置文件的位置,您可以执行以下操作:

      AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
      

      如果您想要对该文件的引用,那么您可以这样做:

      ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      

      【讨论】:

      • 它会将文件作为配置文件返回吗?或者它会返回路径?
      • @ffayyaz: 它返回 web.config 文件的完整物理路径
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2011-08-21
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多