基于这里的这个问题并使用此处找到的代码,我正在尝试加载作为嵌入式资源的视图在单独的 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
,比如罗伯康纳利的/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
时被传入。我是否搞砸了我的路线,或者我期待这种情况发生是对的?
- 必须在
Global.asax
Application_Start
处理程序中注册VirtualPathProvider
。 - 必须使用特殊路径调用 DLL 中的视图,如下所示:
return View("~/Plugin/YOURDLL.dll/FULLNAME_YOUR_VIEW.aspx");
下面是一篇包含可下载代码示例的文章,演示了这一点:
http://www.wynia.org/wordpress/2008/12/aspnet-mvc-plugins/
内置的WebFormsViewEngine使用VirtualPathProviders,因此如果您编写VPP并注册它,则无需对视图引擎进行任何更改。