在VirtualPathProvider中未调用MVC GetFile方法

时间:2014-02-21 11:34:09

标签: asp.net-mvc virtualpathprovider razorengine

我创建了一个VirtualPathProvider来读取部分视图(来自Azure存储)但是当我到达我希望VirtualPathProvider寻找视图的页面(在Azure存储中)时,它不会调用GetFile方法。因此它找不到文件。 FileExists方法确实被调用并返回true。

这是包含我想要加载的视图的页面:

@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>

以下是加载视图的控制器方法:

    public ActionResult Interactive_Stock_Report()
    {

        ControlData cd = GetControlData();

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

最后这里是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!!   
    }
}
}

0 个答案:

没有答案