我尝试创建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< TModel>派生。
尝试使用以下方式显示部分视图时:
@{ Html.RenderAction("Latest", "Blog"); }
有没有办法解决它?
或者是否有更容易的方式来存储DLL的视图?
答案 0 :(得分:6)
发生这种情况的原因是因为您现在正在提供标准~/views/web.config
不再适用的未知位置的剃刀视图。因此,您可以在自定义视图中放置@inherits System.Web.Mvc.WebViewPage
,但这可能会非常麻烦。
您可以结帐the following article。