基于此问题here并使用找到的代码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
有什么建议吗?
UPDATE:当说出http://localhost/plugin/admin
的路由请求到达VirtualFileProvider时,它最后没有附加任何View。因此,在VirtualFileProvider
的Open方法中,~/plugin/admin
的虚拟路径在我应该是~/plugin/admin/Index.aspx
时传入,如上面的路线中所定义的那样。我是否搞砸了我的路线,或者我是否正确期待这种情况发生?
答案 0 :(得分:23)
VirtualPathProvider
Global.asax
处理程序中注册Application_Start
。return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");
这是一篇包含可下载代码示例的文章,演示了这一点:
答案 1 :(得分:4)
内置的WebFormsViewEngine使用VirtualPathProviders,因此如果您编写VPP并注册它,则无需对视图引擎进行任何更改。