本文介绍了如何在网络环境更换OpenExeConfiguration(asp.net的MVC 1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK所以我们当前正在使用OpenExeConfiguration读取配置文件的东西,然而这并没有在网络环境中运行时的工作。

OK so we have something that is currently using OpenExeConfiguration for reading a config file, however this doesn't work when running in the web context.

我已经尝试了各种编程打开web.config中不同的方式,但我似乎无法得到它的读取正确的web.config文件。如果它的事项我目前调试它在VS 2008中。

I've tried a variety of different ways of opening the web.config programmatically but I can't seem to get it to read the correct web.config file. In case it matters I am currently debugging it in VS 2008.

1. config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);

2. config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = "web.config" }, ConfigurationUserLevel.None);

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

4. config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);

5.  System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);

它要么打开了错误的配置文件(无论是机器的配置,或者VS /IDE/Web.config)或抱怨错误:

It either opens up the wrong config file (either the machine config, or the VS /IDE/Web.config) or complains about the error:

{System.Configuration.ConfigurationErrorsException:发生错误加载配置文件:未能映射路径'/'。 ---> System.InvalidOperationException:未能映射路径'/'.

{System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Failed to map the path '/'. ---> System.InvalidOperationException: Failed to map the path '/'.

谢谢!

编辑 -
确定这样的组合

Edit - OK so a combination of

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

和运行Visual Studio 2008作为管理员的工作。我希望我们不会碰上安全/权限问题,当我们部署到我们的Web服务器/客户端环境!

AND running Visual Studio 2008 As Administrator worked. Am hoping we don't run into security/permission issues when we deploy to our web server / client environments!

推荐答案

所以,最后我用这个code(必须处理Web应用程序是否运行,或者如果我们的单元测试code的运行)。

So in the end I used this code (had to handle whether the web application was running, or if our unit test code was running).

System.Configuration.Configuration config = null;
                    if (System.Web.HttpContext.Current != null && !System.Web.HttpContext.Current.Request.PhysicalPath.Equals(string.Empty))
                        config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                    else
                        config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

还必须运行Visual Studio在管理员模式下 - 我发现你可以设置你的快捷方式的属性,因此你不需要在Windows 7中,每次记得要点击右键并以管理员身份运行:)

Also have to be running Visual Studio in Administrator mode - which I found out you can set as a property on your shortcut so you don't need to remember each time in Windows 7 to right click and run as administrator :)

这篇关于如何在网络环境更换OpenExeConfiguration(asp.net的MVC 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 21:12