I have written a custom VirtualPathProvider (source here) which will return content from EmbeddedResources, or from the original file if it has been told where to find it (this
I had the same problem and finally got it working by using a custom RazorHost. It seems that the physical file location is resolved using the HostingEnvironment.MapPath()
method which does not return the correct result for embedded files.
What I did:
public class MyCustomRazorHostFactory : WebRazorHostFactory
{
public override System.Web.WebPages.Razor.WebPageRazorHost CreateHost( string virtualPath, string physicalPath )
{
// Implementation stolen from MvcRazorHostFactory :)
var host = base.CreateHost( virtualPath, physicalPath );
if( !host.IsSpecialPage )
{
return new MyCustomRazorHost( virtualPath, physicalPath );
}
return host;
}
}
public class MyCustomRazorHost : MvcWebPageRazorHost
{
public MyCustomRazorHost( string virtualPath, string physicalPath )
: base( virtualPath, physicalPath )
{
if( MyMagicHelper.IsEmbeddedFile( virtualPath ) )
{
PhysicalPath = MyMagicHelper.GetPhysicalFilePath(virtualPath);
}
}
}
// Simplified for demonstration purpose
public static class MyMagicHelper
{
public static bool IsEmbeddedFile(string virtualPath)
{
// ... check if the path is an embedded file path
}
public static string GetPhysicalFilePath(string virtualPath)
{
// ... resolve the virtual file and return the correct physical file path
}
}
As a last step you need to tell ASP.NET which host factory it should use. This is done in the web.config:
I know my answer comes a bit late but hopefully someone else can make use of it when stumbling across this question as I did. :)