– ‘The application relative virtual path ‘~/’ is not allowed here’ in unit test on 2K3 build server but not XP/Vista dev machines

I'm mocking out HttpRequestBase so I can create an HtmlHelper in test code and write tests around HtmlHelper extension methods. I do this as part of the setup code:

httpRequestBase.Stub(h => h.ApplicationPath).Return("~/");
httpRequestBase.Stub(h => h.AppRelativeCurrentExecutionFilePath).Return(appPath);
httpRequestBase.Stub(h => h.PathInfo).Return("");

which on two dev machines (one running XP, one Vista 64-bit) works fine. However, on a Windows Server 2003 build machine the test fails when I call RouteLink() on the HtmlHelper, like this:

System.ArgumentException : The application relative virtual path '~/' is not allowed here.
at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)

In both cases I'm using NUnit 2.4.8 and the NUnit GUI as the test runner. Everything else is identical except the OS as far as I can tell: same version of RhinoMocks (3.5), same version of ASP.NET MVC (RTM). I tried copying the binaries across from a dev machine rather than using the ones on the build machine, and it makes no difference.

When I change the first line in the setup code to this:

httpRequestBase.Stub(h => h.ApplicationPath).Return("/");

the test passes on all machines.

Any idea why?

Best Solution

"~/" is not a valid value for ApplicationPath. The whole point of the "~/" syntax is to allow you to specify URLs that are relative to ApplicationPath.

For a rooted site, the value should be "/". For sites in virtual directories, it should be a value like "/mysite".