使用 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
如我上面的路线中所定义。 我是否搞乱了我的路线,或者我对这种情况的预期是否正确?
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
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;
}
}
Here are my routes:
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" });
In my AssemblyResourceProvider
I'm checking to see if the path starts ~/plugin/
and then using the dll filename convention plugin.{controller}.dll
Any suggestions?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Global.asax
Application_Start
处理程序中注册您的VirtualPathProvider
。return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");
这里有一篇文章,其中包含可下载的代码示例,演示了这一点:
http://www.wynia.org/wordpress/2008/ 12/aspnet-mvc-plugins/
VirtualPathProvider
within theGlobal.asax
Application_Start
handler.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/
内置的WebFormsViewEngine使用VirtualPathProviders,因此如果您编写VPP并注册它,则不需要对视图引擎进行任何更改。
The built-in WebFormsViewEngine uses VirtualPathProviders, so if you write a VPP and register it, you won't need to make any changes to the view engine.