1

Currently we use plugins with razorgenerator. This works ok, but when the view needs to be editted it needs to be compiled every time. This makes us less productive then wanted. I was working on a custom razor viewengine to solve this. To get it working I need to set the view for the area's to a local directory.

I tried

public class PluginRazorViewEngine : RazorViewEngine
{
    public PluginRazorViewEngine()
    {
       var x = this.AreaViewLocationFormats.ToList();
        x.Insert(0, "C:/XXXXX/Areas/Module/Views/{1}/{0}.cshtml");
        this.AreaViewLocationFormats = x.ToArray();
     }
 }

Though this gives the error:

   The relative virtual path 'C:/XXXXX/Areas/Module/Views/Items/Index.cshtml' is not allowed here.

Is it possible to set the viewpath outside of the project?

Patrick
  • 2,630
  • 4
  • 29
  • 54

1 Answers1

1

I could not find exactly where that error is thrown but I believe you cannot set a path outside of the application the way you are trying to. The RazorViewEngine inherits from BuildManagerViewEngine which in turn inherits from VirtualPathProviderViewEngine. The VirtualPathProviderViewEngine uses the VirtualPathProvider of the host environment.
Thus, it seems you cannot use a direct path and are required to pass in a Virtual path or you could rewrite the underlying layers yourself by directly inheriting from IViewEngine which is what VirtualPathProviderViewEngine inherits from (see image below).

I might be mistaken but I also believe this is enforcing the same principle as specified here that you cannot deliver content from outside the site path.

Have you tried using a relative path to see if that gets mapped correctly. A relative path dependent on the application's root?

Looking through the source of VirtualPathProviderViewEngine you will also notice that the FileExists method of the VirtualPathProvider class is used extensively to find the requested razor file.

FileExists method of VirtualPathProviderViewEngine

Inheritance hierarchy of RazorViewEngine

Source of inheritance diagram: http://theshravan.net/blog/configure-the-views-search-locations-in-asp-net-mvc/

Community
  • 1
  • 1
Judge Bread
  • 501
  • 1
  • 4
  • 13