4

I have created a VirtualPathProvider to read Partial views (from Azure Storage) but when I get to the page where I want the VirtualPathProvider to go looking for a view (in Azure Storage), it doesn't call the GetFile method. Therefore it doesnt find the file. The FileExists method does get called and returns true.

Here is the Page that contains the view that I want loaded:

@model VTSMVC.Models.Controls.ControlData

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@{
    ViewBag.Title = @Model.PageTitle;
}

<h1>@ViewBag.Title</h1>

<div class="stockReportContainer">

    @Html.Partial(@Model.ViewCompName, @Model)

</div>

Here is the controller method that loads the view:

    public ActionResult Interactive_Stock_Report()
    {

        ControlData cd = GetControlData();

          //this is the view returned on initial load
            return View(cd);
    }

Finally here is the VirtualPathProvider :

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;

namespace VTSMVC.Helpers.Utilities
{
public class BlobStorageVirtualPathProvider : VirtualPathProvider
{
    public override bool FileExists(string virtualPath)
    {
        // Check if the file exists on blob storage 
        string cleanVirtualPath = virtualPath.Replace(@"~/Views/Shared/", "");
        if (BlobExists(cleanVirtualPath))
        {
            return true;
        }
        else
        {
            return Previous.FileExists(virtualPath);
        }
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        //This gets called but only at the points where I don't need it 
        //ie it gets called where where I want Azure 
        string cleanVirtualPath = virtualPath.Replace(@"~/Views/Shared/", "");
        if (BlobExists(cleanVirtualPath))
        {
            return new BlobStorageVirtualFile(virtualPath, this);
        }
        else
        {
            return Previous.GetFile(virtualPath);
        }
    }

    public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
    }

    private bool BlobExists(string cleanVirtualPath)
    {
        switch(cleanVirtualPath)
        {
            //just doing a simple test for now
            case "_MyStorageViewFile.cshtml":
                return true;
            default:
                return false;
        }
    }
}

public class BlobStorageVirtualFile : VirtualFile
{
    protected readonly BlobStorageVirtualPathProvider parent;

    public BlobStorageVirtualFile(string virtualPath, BlobStorageVirtualPathProvider parentProvider) : base(virtualPath)
    {
        parent = parentProvider;
    }

    public override System.IO.Stream Open()
    {
        //Open Method blah blah blah - 
        //not getting to this point since GetFile doesnt get called!!   
    }
}
}
6
  • Did you ever solve this problem? I've run into the same thing. Commented Jun 16, 2014 at 4:43
  • I gave up with trying to solve the above and tackled it a different way. What are you trying to achieve? Commented Jun 16, 2014 at 22:38
  • I'm trying to store razor views in a database, and show them only for certain routes. So site.com/CMS/Index would look for a page named 'Index' in a database table, get the razor code and show it. Commented Jun 16, 2014 at 23:29
  • I think my equivalent which may or may not work for you is convert the pages you were going to store in a database to partials and then load the right partial depending on the result of a function on the parent page. By function I mean you can define "@functions" on the page below your html and other razor. Or instead of functions you could have a switch statement driven by a value from your model to load the appropriate partial. Commented Jun 17, 2014 at 8:31
  • But wouldn't that require that the partials are still hard coded views in .cshtml files? I'm still opting for the VirtualPathProvider way, it's just strange how it won't work in mvc 5, but earlier versions. Commented Jun 17, 2014 at 14:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.