使用VirtualPathProvider从DLL加载ASP.NET MVC视图

基于这里的问题,并使用在这里find的代码我试图加载视图是在一个单独的DLL项目中embedded资源,原始问题的作者说,他已经成功做到这一点 – 但我不能得到它的工作看起来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我正在检查path是否启动~/plugin/ ,然后使用dll文件名约定plugin.{controller}.dll

有什么build议么?

更新:当路由请求说http://localhost/plugin/admin正在得到的VirtualFileProvider它没有任何视图附加在最后。 所以在VirtualFileProvider的Open方法中,当~/plugin/admin/Index.aspx在我上面的路由中定义时, ~/plugin/admin的虚拟path被传入。 我弄乱了我的路线还是我期待这种事情发生的权利?

  1. 您必须在Global.asax Application_Start处理程序中注册您的VirtualPathProvider
  2. 你必须使用如下的特殊path调用DLL中的视图: return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");

这里有一个可下载的代码示例文章,演示了这一点:

http://www.wynia.org/wordpress/2008/12/aspnet-mvc-plugins/

内置的WebFormsViewEngine使用VirtualPathProviders,因此如果您编写VPP并注册它,则不需要对视图引擎进行任何更改。