0

I am using ASP.NET MVC5 and I would like to render a view from a folder that is outside the application folder. I tried registering my own custom the VirtualPathProvider and I even created my own VirtualPathProviderViewEngine to support rendering pure html pages. I have the latter working but cannot get the former to work. When I navigate to the route in question, I want MVC to check the internal Views folder for the View and then if it is not found I want it to look in the external folder.

When I step through the code, FileExists gets called for files that are in the Views folder and then the ViewEngine code runs but for a View that lives externally, the FileExists check runs and then I get a 404 on the screen. It does not ever get into the ViewEngine code. I know I am missing something simple here.

I am attaching a screenshot of what the sample folder structure would be. Any help would be greatly appreciated. FolderStructure

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
abraganza
  • 135
  • 2
  • 9
  • I may be wrong, but I could see this as being a security feature preventing malicious scripts from being able to access anything on the server, instead of only within the application's folders. – krillgar Sep 30 '16 at 19:21
  • I see your point but I am pretty sure that I have access to the entire MVC stack and as the developer of the app, I should have the ability to look for specific files in a location of my choosing. – abraganza Sep 30 '16 at 19:26
  • Potential duplicate target: http://stackoverflow.com/q/5788631/1195056 – krillgar Sep 30 '16 at 19:29
  • I want an external location to house my custom views (and only custom views) that survive an upgrade or an uninstall. I need custom created content to stick around. I do not want to put it in the application folder. I want to put it in a folder that is in accessible from IIS but is traversable from within the application, by me. I am pretty sure it is doable. I do not want to create anything extra in IIS. – abraganza Sep 30 '16 at 19:33

1 Answers1

3

You can override the VirtualPathProvider and the VirtualFile Check this link for an Example

Laurent Lequenne
  • 902
  • 5
  • 13
  • I already tried this. FileExists is called but GetFile is never called. I even overrode the VirtualPathProviderViewEngine but it wont find my files outside the the main folder. My files are on the filesystem, not in a database, as is detailed in the example you provided. I appreciate the assistance. – abraganza Oct 01 '16 at 23:36
  • 1
    You have to put the right logic in the FileExists and GetFile, the fileexists should map your virtual file (the string that is defined as a view :)) to an existing file. if that map exists, and you return true, the Getfile should read the view from anywhere you need. – Laurent Lequenne Oct 02 '16 at 01:47
  • Thanks for the insight @laurent-lequenne! I believe I got it figured out. I had to create my on virtualpathprovider, virtualfile and virtual directory and use the methods you mentioned to pull the file content to load it when needed. Although I did not use the example above, it did highlight some key areas that I needed to look and and I was able to load a file from a folder outside of my application as expected. Thanks! – abraganza Oct 03 '16 at 18:55