使用 VirtualPathProvider 从 DLL 加载 ASP.NET MVC 视图 [英] Using VirtualPathProvider to load ASP.NET MVC views from DLLs

查看:23
本文介绍了使用 VirtualPathProvider 从 DLL 加载 ASP.NET MVC 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这个问题这里并使用找到的代码这里 我正在尝试加载在单独的 DLL 项目中嵌入资源的视图,原始问题的作者说他已经成功地做到了这一点 - 但我无法让它工作,因为看起来 MVC 视图引擎正在拦截请求并且仍在查看视图的文件系统.例外:

Based on this question here and using code found here I'm trying to load views that are embedded resources in a separate DLL project, and the original question's author says he has had success doing this - but I can't get it to work as it seems the MVC view engine is intercepting the request and still looking at the file system for the view. Exception:

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 结构之一,如下所示:

I am using a CustomViewEngine, like Rob Connery's /App structure one as follows:

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

In my AssemblyResourceProvider I'm checking to see if the path starts ~/plugin/ and then using the dll filename convention plugin.{controller}.dll

有什么建议吗?

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

UPDATE: By the time the routed request for say http://localhost/plugin/admin is getting to the VirtualFileProvider it doesn't have any View attached at the end. So in the VirtualFileProvider's Open method the virtual path of ~/plugin/admin is being passed in when it should be ~/plugin/admin/Index.aspx as defined in my route above. Have I messed up my routes or am I right to be expecting this to happen?

推荐答案

  1. 您必须在 Global.asax Application_Start 处理程序中注册您的 VirtualPathProvider.
  2. 您必须使用如下特殊路径调用 DLL 中的视图:return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");
  1. You must register your VirtualPathProvider within the Global.asax Application_Start handler.
  2. You must call the view in your DLL using the special path like so: return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");

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

Here's an article with downloadable code sample that demonstrates this:

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

这篇关于使用 VirtualPathProvider 从 DLL 加载 ASP.NET MVC 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆