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

基于此处的问题并使用此处找到的代码,我试图将作为嵌入式资源的视图加载到一个单独的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上面我的路线中的定义传递虚拟路径。我是否弄乱了路线,还是期待发生这种情况?


MYYA
浏览 539回答 3
3回答

当年话下

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

慕森卡

第一次FileExists调用发生在控制器运行之前,并且必须返回false,否则IIS会尝试将其用作静态文件。当控制器请求视图时,对实际aspx文件的请求稍后出现。
打开App,查看更多内容
随时随地看视频慕课网APP