如何从类库项目中加载视图? [英] How to load views from a Class Library project?
问题描述
我尝试创建一个 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);
}
返回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))
返回新的AssemblyResourceVirtualFile(virtualPath);
else
return base.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath,
IEnumerable virtualPathDependencies,DateTime utcStart)
{
if(IsAppResourcePath(virtualPath))
返回null;
else
返回base.GetCacheDependency(virtualPath,virtualPathDependencies,utcStart);
}
}
我的控制器
return View(〜/ App_Resource / Blog.DLL / Blog.Views.Blog.Latest.cshtml);
它找到该视图,但是我最终得到这个错误:
... view.cshtml必须来自WebViewPage或WebViewPage< TModel>。
当尝试使用以下方式显示部分视图时:
@ {Html.RenderAction(Latest,博客);
有没有办法解决? / p>
还是有一个更容易的方式来存储DLL上的视图?
发生这种情况的原因是因为您现在正在从未知位置提供剃须刀视图,标准〜/ views / web.config
不再适用。所以你可以在你的自定义视图中放入一个 @inherits System.Web.Mvc.WebViewPage
,但这可能是一个麻烦。
您可以退房以下文章。
I tried to create a VirtualPathProvider
and set the view as an embedded resource.
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;
}
}
And
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);
}
}
My controller
return View("~/App_Resource/Blog.DLL/Blog.Views.Blog.Latest.cshtml");
It does find the view, but I end up getting this error:
... view.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.
When trying to show the partial view using:
@{ Html.RenderAction("Latest", "Blog"); }
Is there a way to fix it?
Or is there an easier way to store views on a DLL?
The reason this happens is because you are now serving the razor views from unknown locations the standard ~/views/web.config
no longer applies. So you could put a @inherits System.Web.Mvc.WebViewPage
inside your custom views but it could be quite a hassle.
You may checkout the following article.
这篇关于如何从类库项目中加载视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!