Application relative virtual path error

I’ve got a self-hosted razor windows service that works fine when compiled in debug mode and run in console, but when compiled as release and started as a service I get this error on my default razor page:

The application relative virtual path ‘~/Content/bootstrap.min.css’ cannot be made absolute, because the path to the application is not known.

at System.Web.VirtualPath.get_VirtualPathString() at ASP.Views.Shared.Layout.Execute() at ServiceStack.Razor.ViewPage.WriteTo(StreamWriter writer) at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, Object model, IRazorView pageInstance, Func1 layout) at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, Object model, IRazorView pageInstance, Func1 layout) at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPage(IRequest httpReq, IResponse httpRes, Object model, RazorPage razorPage) at ServiceStack.Razor.RazorHandler.ProcessRequest(IRequest httpReq, IResponse httpRes, String operationName) at ServiceStack.Host.Handlers.HttpAsyncTaskHandler.<>c__DisplayClass9_0.b__0() at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute()

I’ve tried adding this to hostconfig (with and without) with no effect:

    var config = new HostConfig
    {
        WebHostPhysicalPath =  "~".MapServerPath()  
    };

Any ideas?

Make sure your static files are either set to “Copy if newer” so they get copied in your /bin/Release directory.

To find out what RootDirectoryPath that your ServiceStack instance is looking at, enable DebugMode, e.g:

SetConfig(new HostConfig { DebugMode = true });

and then go to ?debug=requestinfo.

You want to make sure your static files are available in that directory otherwise you can set your static files embedded resources so they’re available from within the .exe.

Not sure where you’re using '~/Content/bootstrap.min.css' in your Razor views but you can just use '/Content/bootstrap.min.css' which will look for the file from the root folder.

Thanks for the tip, the ~/ were in the _layout.cshtml files for the script sources. Removing them did fix the issue, but trying to understand why it works in debug but not in release.

The physical path to the root was shown correct in both release and debug mode. The files are deployed (not running under VS2015) in the same path and all I’m doing different is compiling in release and debug mode.

Release mode of a WindowsService project is run as a Windows Service which is different to when you’re debugging which is run as a Console App.

The Current Working Directory of each are different, I still don’t know what the Razor HTML tag you’re using for ‘~/Content/bootstrap.min.css’ in, but I’m assuming you’re using a System.Web.Razor feature to resolve the paths which could be looking at the Current Working Directory, which for a Console App is the same as the RootDirectoryPath.