2014-02-20 30 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文件中。

相关问题