WebConfigurationManager.OpenWebConfiguration throws Failed to map the path ‘/’.

Create a ASP.NET Web Site and put this piece of code into Page_Load method:

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

There are good chances you’ll get Failed to map the path ‘/’. exception. The error is a peculiar one, even more so because the code is straight from the MSDN documentation. It happened to me when I’ve upgraded my ASP.NET Web Site Visual Studio 2005 project to 2008 (still targeting .net 2.0 framework) one.

After scratching my head for a while and googling around I’ve found the solution: run Visual Studio as administrator. Right, I’ve set my Visual Studio 2005 to run always as administrator and thus it worked before. After upgrading to Visual Studio 2008 the problem began to manifest – because I haven’t yet set Visual Studio 2008 to run as administrator by default nor did I run it as administrator explicitly. So, to make it work you should run Visual Studio as administrator. Which is a problem because many people would use administrator privileges when developing (i.e. writing into Program Files folder – don’t do it!). And when they’ll deploy application to normal users it will throw exceptions one after the another.

So, be cautious when you are developing under administrator privileges.

UPDATE: I run Vista x86 OS. Not sure, but the same problem might be happening on XP/2003, too.

3 thoughts on “WebConfigurationManager.OpenWebConfiguration throws Failed to map the path ‘/’.

Leave a Reply