If you’re running unit tests in an ASP.NET web app on code accessing settings from the web.config, then you might have some code like:

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;

That will work fine when you run the project, but your test will fail and give you the error ‘The application relative virtual path ‘~/’ is not allowed here.’. Fortunately, there is the HttpRuntime.AppDomainAppVirtualPath property you can use for the path, which works in tests as well as when running. (In tests this value is null.) So you code would then look like:

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
 HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;

And your test will work.

 

One Response to Testing: The application relative virtual path ‘~/’ is not allowed here.

  1. don says:

    you save my job man! thank you! =)