40

基于this question 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 时,它最后没有附加任何视图。因此,在VirtualFileProvider's Open 方法~/plugin/admin中,当它应该~/plugin/admin/Index.aspx在我上面的路线中定义时,它被传入。我是否搞砸了我的路线,或者我期望这种情况发生是正确的?

4

2 回答 2

24
  1. 您必须VirtualPathProviderGlobal.asax Application_Start处理程序中注册您的。
  2. 您必须使用特殊路径调用 DLL 中的视图,如下所示:return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");

这是一篇带有可下载代码示例的文章,演示了这一点:

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

于 2008-10-26T17:58:22.380 回答
4

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

于 2008-10-26T15:07:48.153 回答