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

 2023-02-17    415  

问题描述

基于这个问题这里并使用代码 here 文件系统.例外:

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’s/app结构,如下所示:

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

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的打开方法中,~/plugin/admin的虚拟路径在上面的路线中定义时,它会在应该~/plugin/admin/Index.aspx时传递.我搞砸了我的路线,或者我要期待这种情况是正确的吗?

推荐答案

  1. 您必须在Global.asax Application_Start处理程序中注册VirtualPathProvider.
  2. 您必须使用如此的特殊路径调用DLL中的视图:return View(“~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx”);

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

其他推荐答案

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

以上所述是小编给大家介绍的使用VirtualPathProvider从DLLs加载ASP.NET MVC视图,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/34263.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。