I created VirtualPathProvider to read partial views (from Azure Storage), but when I get to the page where I want VirtualPathProvider to go look for the view (in Azure Storage), it does not call the GetFile method. Therefore, it does not find the file. The FileExists method calls and returns true.
Here is the page containing the view I want to download:
@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();
return View(cd);
}
Finally, here is 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)
{
string cleanVirtualPath = virtualPath.Replace(@"~/Views/Shared/", "");
if (BlobExists(cleanVirtualPath))
{
return true;
}
else
{
return Previous.FileExists(virtualPath);
}
}
public override VirtualFile GetFile(string virtualPath)
{
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)
{
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()
{
}
}
}
source
share