2014-02-20 55 views
1

我已經定義了一個VirtualPathProvider,用於從Azure存儲獲取cshtml文件以用作部分視圖。使用MVC和Azure存儲使用VirtualPathProvider時出現錯誤

我收到以下錯誤啓動:

在 '〜/瀏覽/首頁/的Index.aspx' 的視圖必須自ViewPage, 的ViewPage,ViewUserControl,或ViewUserControl派生。

看來我的virtualPathProvider正在停止使用剃鬚刀視圖引擎。

這裏是的VirtualPathProvider:

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

namespace VTSMVC.Helpers.Utilities 
{ 
public class BlobStorageVirtualPathProvider:VirtualPathProvider 
{ 
    protected readonly CloudStorageAccount accountInfo; 
    protected readonly CloudBlobContainer container; 
    public CloudBlobContainer BlobContainer 
    { 
     get { return container; } 
    } 

    public BlobStorageVirtualPathProvider() 
    { 

     // Retrieve storage account from connection string. 
     switch (ConfigurationManager.AppSettings["WebCloud"]) 
     { 
      case "Cloud": 
       // Retrieve storage account from connection string. 
       accountInfo =  CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); 
       break; 

      case "Web": 
      default: 
       accountInfo = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString); 
       break; 
     } 

     // Create the blob client. 
     CloudBlobClient blobClient = accountInfo.CreateCloudBlobClient(); 

     // Retrieve a reference to a container. 
     container = blobClient.GetContainerReference("stockreports"); 
    } 

    public override bool FileExists(string virtualPath) 
    { 
     // Check if the file exists on blob storage 
     //string cleanVirtualPath = virtualPath.Replace("~", "").Substring(1); 
     string cleanVirtualPath = virtualPath; 
     if (container.GetBlockBlobReference(cleanVirtualPath) != null) 
     { 
      return true; 
     } 
     else 
     { 
      return Previous.FileExists(virtualPath); 
     } 

    } 

    public override VirtualFile GetFile(string virtualPath) 
    { 
     // Check if the file exists on blob storage 
     //string cleanVirtualPath = virtualPath.Replace("~", "").Substring(1); 
     string cleanVirtualPath = virtualPath; 
     if (container.GetBlockBlobReference(cleanVirtualPath) != null) 
     { 
      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; 
    } 
} 

public class BlobStorageVirtualFile : VirtualFile 
{ 
    protected readonly BlobStorageVirtualPathProvider parent; 

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

    public override System.IO.Stream Open() 
    { 
     MemoryStream ms = new MemoryStream(); 

     try 
     { 
      //string cleanVirtualPath = this.VirtualPath.Replace("~", "").Substring(1); 
      string cleanVirtualPath = this.VirtualPath; 

      // Retrieve reference to a blob 
      CloudBlockBlob blockBlob = parent.BlobContainer.GetBlockBlobReference(cleanVirtualPath); 

      //Retrive the memorystream 
      blockBlob.DownloadToStream(ms); 
     } 
     catch (Exception) 
     { 
      // TODO: log your error messages here 
     } 
     return ms; 
    } 
} 
} 

這裏是我的Global.asax

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Hosting; 
using System.Web.Mvc; 
using System.Web.Optimization; 
using System.Web.Routing; 
using VTSMVC.Helpers.Utilities; 

namespace VTSMVC 
{ 
public class MvcApplication : System.Web.HttpApplication 
{ 
protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 

    // Register the virtual path provider with ASP.NET 
    System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new BlobStorageVirtualPathProvider()); 
} 
} 
} 

到我的Azure存儲連接似乎我已經用它來保存工作。

有人知道什麼是錯?

+0

您是否找到了解決方法? –

+0

@AntRadha - 我放棄了使用VirtualPathProvider。我不認爲我曾經解決過它。 –

回答

0

對於其他用戶的利益:

頁需要從System.Web.Mvc.ViewPage或類似基類型來推導。這可以使用.cshtml文件中的@inherits完成。

我們通常不需要的原因是因爲Views文件夾包含一個web.config文件,該文件將默認頁面基本類型設置爲WebViewPage。或者,您可以從此web.config文件中複製該部分,並將其放入主Web.config文件中。

相關問題