gpt4 book ai didi

Asp.net MVC VirtualPathProvider View 解析错误

转载 作者:行者123 更新时间:2023-12-04 06:48:06 24 4
gpt4 key购买 nike

我正在为 Asp.net MVC 2 开发一个插件系统。我有一个包含 Controller 和 View 作为嵌入式资源的 dll。

我使用 StructureMap 扫描 Controller 的插件 dll,然后我可以将它们拉出并在需要时实例化它们。这工作正常。然后我有一个 VirtualPathProvider,我改编自 this post

public class AssemblyResourceProvider : VirtualPathProvider
{
protected virtual string WidgetDirectory
{
get
{
return "~/bin";
}
}

private bool IsAppResourcePath(string virtualPath)
{
var checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith(WidgetDirectory, StringComparison.InvariantCultureIgnoreCase);
}

public override bool FileExists(string virtualPath)
{
return (IsAppResourcePath(virtualPath) || base.FileExists(virtualPath));
}

public override VirtualFile GetFile(string virtualPath)
{
return IsAppResourcePath(virtualPath) ? new AssemblyResourceVirtualFile(virtualPath) : base.GetFile(virtualPath);
}

public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies,
DateTime utcStart)
{
return IsAppResourcePath(virtualPath) ? null : base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}

internal class AssemblyResourceVirtualFile : VirtualFile
{
private readonly string path;

public AssemblyResourceVirtualFile(string virtualPath)
: base(virtualPath)
{
path = VirtualPathUtility.ToAppRelative(virtualPath);
}

public override Stream Open()
{
var parts = path.Split('/');
var resourceName = Path.GetFileName(path);

var apath = HttpContext.Current.Server.MapPath(Path.GetDirectoryName(path));
var assembly = Assembly.LoadFile(apath);
return assembly != null ? assembly.GetManifestResourceStream(assembly.GetManifestResourceNames().SingleOrDefault(s => string.Compare(s, resourceName, true) == 0)) : null;
}
}

VPP 似乎也工作正常。 View 被找到并被拉出到一个流中。然后我收到一个解析错误 Could not load type 'System.Web.Mvc.ViewUserControl<dynamic>'.我在之前的任何可插入 View 示例中都没有提到。为什么我的 View 在这个阶段不能编译?

谢谢你的帮助,

伊恩

编辑:

越来越接近答案,但不太清楚为什么事情没有编译。根据我检查版本的评论,所有内容都在 V2 中,我相信在 V2 中引入了动态,所以这很好。我什至没有安装 V3,所以不可能那样。但是,如果删除 <dynamic>,我可以渲染 View 共。

所以 VPP 可以工作,但前提是 View 不是强类型或动态的

这对于强类型场景是有意义的,因为类型在动态加载的 dll 中,因此 View 引擎不会意识到它,即使 dll 在 bin 中。有没有办法在应用程序启动时加载类型?考虑使用 MEF 而不是我定制的 Structuremap 解决方案。你怎么认为?

最佳答案

允许解析强类型 View 的设置在 ~/Views/Web.Config 中。当 View 引擎使用虚拟路径提供程序时,它不在 View 文件夹中,因此不会加载这些设置。

如果将 Views/Web.Config 的 pages 节点中的所有内容复制到根 Web.Config,强类型 View 将被正确解析。

关于Asp.net MVC VirtualPathProvider View 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3507177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com