【问题标题】:Using VirtualPathProvider to load ASP.NET MVC views from DLLs使用 VirtualPathProvider 从 DLL 加载 ASP.NET MVC 视图
【发布时间】:2010-09-19 05:09:25
【问题描述】:

基于这个问题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

有什么建议吗?

更新:http://localhost/plugin/admin 的路由请求到达 VirtualFileProvider 时,它最后没有附加任何视图。所以在VirtualFileProvider 的Open 方法中,~/plugin/admin 的虚拟路径被传入,而它应该是~/plugin/admin/Index.aspx,正如我上面的路由中所定义的那样。我是否搞砸了我的路线,还是我期望这种情况发生是正确的?

【问题讨论】:

  • 第一次 FileExists 调用发生在控制器运行之前,并且必须返回 false 否则 IIS 将尝试将其作为静态文件提供服务。对实际 aspx 文件的请求稍后会在控制器请求视图时出现。

标签: asp.net asp.net-mvc virtualpathprovider


【解决方案1】:
  1. 您必须在 Global.asax Application_Start 处理程序中注册您的 VirtualPathProvider
  2. 您必须使用特殊路径调用 DLL 中的视图,如下所示:return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");

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

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

【讨论】:

【解决方案2】:

内置的WebFormsViewEngine使用VirtualPathProviders,所以如果你写一个VPP并注册它,你不需要对视图引擎做任何改变。

【讨论】:

    猜你喜欢
    • 2022-10-07
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多