当前位置:  开发笔记 > 后端 > 正文

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

如何解决《使用VirtualPathProvider从DLL加载ASP.NETMVC视图》经验,为你挑选了1个好方法。

基于这个问题,在这里和使用代码发现在这里,我试图加载嵌入资源在一个单独的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时,它最后没有附加任何View.因此,在VirtualFileProviderOpen方法中,虚拟路径将~/plugin/admin被传入,而它应该~/plugin/admin/Index.aspx在我上面的路由中定义.我搞砸了我的路线还是我没想到会发生这种情况?



1> jmcd..:

    您必须VirtualPathProviderGlobal.asax Application_Start处理程序中注册.

    您必须使用特殊路径调用DLL中的视图,如下所示: return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");

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

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

推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有