我试图创建一个VirtualPathProvider
并将视图设置为嵌入资源。
class AssemblyResourceVirtualFile : VirtualFile
{
string path;
public AssemblyResourceVirtualFile(string virtualPath)
: base(virtualPath)
{
path = VirtualPathUtility.ToAppRelative(virtualPath);
}
public override System.IO.Stream Open()
{
string[] parts = path.Split('/');
string assemblyName = parts[2];
string resourceName = parts[3];
assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
var assembly = Assembly.LoadFile(assemblyName);
if (assembly != null)
{
return assembly.GetManifestResourceStream(resourceName);
}
return null;
}
}
以及
public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider
{
public AssemblyResourceProvider() { }
private bool IsAppResourcePath(string virtualPath)
{
String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith("~/App_Resource/", StringComparison.InvariantCultureIgnoreCase);
}
public override bool FileExists(string virtualPath)
{
return (IsAppResourcePath(virtualPath) ||
base.FileExists(virtualPath));
}
public override VirtualFile GetFile(string virtualPath)
{
if (IsAppResourcePath(virtualPath))
return new AssemblyResourceVirtualFile(virtualPath);
else
return base.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath,
IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (IsAppResourcePath(virtualPath))
return null;
else
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
我的控制器
return View("~/App_Resource/Blog.DLL/Blog.Views.Blog.Latest.cshtml");
它确实找到了视图,但我最终得到了这个错误:
…view.cshtml'必须派生自webviewpage或webviewpage。
尝试使用以下命令显示部分视图时:
@{ Html.RenderAction("Latest", "Blog"); }
有办法解决吗?
或者有一种更简单的方法将视图存储在dll上吗?
最佳答案
之所以会发生这种情况,是因为您现在正在为来自未知位置的razor视图提供服务,标准~/views/web.config
不再适用。因此,您可以在自定义视图中放置一个@inherits System.Web.Mvc.WebViewPage
,但这可能会很麻烦。
您可以结帐the following article。