2

I've been trying for days (really, days) to use "external" files (provided from a different server using an ashx handler) as layouts in Razor.

@{
    Layout = "http://someServer/templates.ashx?path=/my/template.cshtml";
}

This gives me an error about the path having to be a virtual one.

I've tried everything I could think of: VirtualPathProviders, custom RazorViewEngines, etc.

Nothing helps, has anyone done this or can someone give me a hint?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Dänu
  • 5,791
  • 9
  • 43
  • 56
  • A VirtualPathProvider should work. The string that gets passed to the VPP will still need to look like a virtual path. – SLaks Jul 05 '11 at 19:46
  • Yeah, the problem is, the VirtualPathProvider somehow overrides the default one, so the engine tries to load all views from the external location (-> with the custom VirtualPathProvider). – Dänu Jul 05 '11 at 19:53
  • Make the custom VPP forward normal paths to the default one. – SLaks Jul 05 '11 at 19:54
  • Ok.. how do I do this? (give it as answer, I'll accept it ;-)) – Dänu Jul 05 '11 at 20:05

1 Answers1

1

Make a VirtualPathProvider that handles virtual paths that start with a magic token and passes all other paths to its Previous property.
For example:

public override VirtualFile GetFile(string virtualPath) {
    if (virtualPath.StartsWith("~/MySpecialTemplateServer"))
        return new MyServerVirtualFile(virtualPath);
    else
        return Previous.GetFile(virtualPath);
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964