Asp.net mvc 将VirtualPathProvider与MVC和Azure存储一起使用时不断收到错误
我已经定义了一个VirtualPathProvider,用于从Azure存储中获取cshtml文件以用作部分视图 我在启动时遇到以下错误: “~/Views/Home/Index.aspx”处的视图必须派生自ViewPage, ViewPage、ViewUserControl或ViewUserControl 我的virtualPathProvider似乎正在停止使用razor视图引擎 以下是VirtualPathProvider:Asp.net mvc 将VirtualPathProvider与MVC和Azure存储一起使用时不断收到错误,asp.net-mvc,asp.net-mvc-4,razor,azure,Asp.net Mvc,Asp.net Mvc 4,Razor,Azure,我已经定义了一个VirtualPathProvider,用于从Azure存储中获取cshtml文件以用作部分视图 我在启动时遇到以下错误: “~/Views/Home/Index.aspx”处的视图必须派生自ViewPage, ViewPage、ViewUserControl或ViewUserControl 我的virtualPathProvider似乎正在停止使用razor视图引擎 以下是VirtualPathProvider: using System; using System.Colle
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存储的连接似乎可以正常工作,因为我使用它来保存
有人知道怎么回事吗?为了其他用户的利益: 页面需要派生自System.Web.Mvc.ViewPage或类似的基本类型。这可以使用.cshtml文件中的@inherits来完成
我们通常不需要这样做的原因是Views文件夹包含一个web.config文件,该文件将默认页面基本类型设置为WebViewPage。或者,您可以从该web.config文件复制该节,并将其放入主web.config文件。您找到解决方案了吗?@AntRadha-我放弃使用VirtualPathProvider。我想我从来没有解决过。