我写了自定义VirtualFile和VirtualPathProvider实现,它们成功获取了部分视图的嵌入式资源。
但是,当我试图渲染它们会产生这个错误:
在“〜/ Succeed.Web / Succeed.Web.Controls.SImporter._SImporter.cshtml”的视图必须从WebViewPage或WebViewPage< TModel>派生。
在渲染部分视图时,在常规视图内部,它如下所示:
Html.RenderPartial("~/Succeed.Web/Succeed.Web.Controls.SImporter._SImporter.cshtml");
是什么原因导致它认为这不是局部视图?
编辑:我发布我的代码为虚拟文件&虚拟文件提供程序实现为任何人绊倒寻找解决方案获得该组件工作。这个问题也将适用于那些基于问题标题。
ere是VirtualFile实现的参考:
public class SVirtualFile : VirtualFile { private string m_path; public SVirtualFile(string virtualPath) : base(virtualPath) { m_path = VirtualPathUtility.ToAppRelative(virtualPath); } public override System.IO.Stream Open() { var parts = m_path.Split('/'); var assemblyName = parts[1]; var resourceName = parts[2]; assemblyName = Path.Combine(HttpRuntime.BinDirectory,assemblyName); var assembly = System.Reflection.Assembly.LoadFile(assemblyName + ".dll"); if (assembly != null) { return assembly.GetManifestResourceStream(resourceName); } return null; } }
VirtualPathProvider:
public class SVirtualPathProvider : VirtualPathProvider { public SVirtualPathProvider() { } private bool IsEmbeddedResourcePath(string virtualPath) { var checkPath = VirtualPathUtility.ToAppRelative(virtualPath); return checkPath.StartsWith("~/Succeed.Web/",StringComparison.InvariantCultureIgnoreCase); } public override bool FileExists(string virtualPath) { return IsEmbeddedResourcePath(virtualPath) || base.FileExists(virtualPath); } public override VirtualFile GetFile(string virtualPath) { if (IsEmbeddedResourcePath(virtualPath)) { return new SVirtualFile(virtualPath); } else { return base.GetFile(virtualPath); } } public override CacheDependency GetCacheDependency( string virtualPath,IEnumerable virtualPathDependencies,DateTime utcStart) { if (IsEmbeddedResourcePath(virtualPath)) { return null; } else { return base.GetCacheDependency(virtualPath,virtualPathDependencies,utcStart); } } }
当然,不要忘记在Application_Start()事件中的项目的Global.asax文件中注册这个新的提供者
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new SVirtualPathProvider());