基于这个问题here并使用代码发现here,我正在try 加载视图,这些视图是一个单独的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 structure one,如下所示:

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

有什么建议吗?

UPDATE:当路由的http://localhost/plugin/admin请求到达VirtualFileProvider时,它的末尾没有附加任何视图.因此,在VirtualFileProvider的Open方法中,~/plugin/admin的虚拟路径被传入,而实际上它应该是~/plugin/admin/Index.aspx,正如我在上面的路由中定义的那样.我是不是搞砸了我的路由,或者我预料到这会发生是对的吗?

推荐答案

  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/

Asp.net相关问答推荐

InvalidOperationException:在程序集上找不到UserSecretsIdAttribute

在 lambda 表达式中否定 Func

在构建时自动停止/重新启动 ASP.NET 开发服务器

无法获取项目引用的依赖项

有没有一种简单的方法可以将对象属性转换为字典

Gridview ItemTemplate 中多个判断字段的最佳技术?

在使用网络服务器加载它们之前,如何编译 Asp.Net Aspx 页面?

如何为角色以及特定用户使用自定义授权属性?

在 asp.net 中为动态 PDF 指定文件名

ASP.NET 上的 WebSockets 教程

如何验证用户在 CheckBoxList 中 Select 了至少一个复选框?

解析器错误消息:无法加载类型网络营销

.NET AJAX 调用 ASMX 或 ASPX 或 ASHX?

由于文件路径太长,网站发布失败

Html.RenderAction 和 Html.Action 的区别

模型是否包含字段而不将其添加到数据库中?

如何使用 jquery 设置单选按钮 Select 的值

如何使用 ConfigurationManager.AppSettings

字体真棒里面asp按钮

Appdomain 回收究竟是什么