基于这个问题here,并使用在这里找到的代码,我正在尝试加载嵌入在另一个DLL项目中的视图资源,原问题的作者表示他已经成功实现了这一点 - 但我无法让它工作,因为似乎MVC视图引擎正在拦截请求并仍然查看文件系统中的视图。异常:
Server Error in '/' Application.
The view 'Index' or its master could not be found. The following locations were searched:
~/Views/admin/Index.aspx
~/Views/admin/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/App/Views/admin/Index.aspx
~/App/Views/admin/Index.ascx
~/App/Views/Shared/Index.aspx
~/App/Views/Shared/Index.ascx
我正在使用一个CustomViewEngine
,就像Rob Connery的/App结构一样:
public class CustomViewEngine : WebFormViewEngine
{
public CustomViewEngine()
{
MasterLocationFormats = new[] {
"~/App/Views/{1}/{0}.master",
"~/App/Views/Shared/{0}.master"
};
ViewLocationFormats = new[] {
"~/App/Views/{1}/{0}.aspx",
"~/App/Views/{1}/{0}.ascx",
"~/App/Views/Shared/{0}.aspx",
"~/App/Views/Shared/{0}.ascx"
};
PartialViewLocationFormats = ViewLocationFormats;
}
}
这是我的路由:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Home", "", new {controller = "Page", action = "Index", id = "Default"});
routes.MapRoute("Default", "Page/{id}", new { controller = "Page", action = "Index", id = "" });
routes.MapRoute("Plugins", "plugin/{controller}/{action}", new { controller = "", action = "Index", id = "" });
routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "ResourceNotFound404" });
在我的 AssemblyResourceProvider
中,我正在检查路径是否以 ~/plugin/
开头,然后使用 dll 文件命名约定 plugin.{controller}.dll
。有什么建议吗? 更新: 当针对
http://localhost/plugin/admin
的路由请求到达 VirtualFileProvider 时,它没有任何 View 附加在末尾。因此,在 VirtualFileProvider
的 Open 方法中,虚拟路径 ~/plugin/admin
被传递进来,而应该是根据我的上面定义的路由 ~/plugin/admin/Index.aspx
。我是搞砸了我的路由还是应该期望这种情况发生?