【发布时间】:2010-09-19 05:09:25
【问题描述】:
基于这个问题here 并使用找到的代码here 我正在尝试在单独的 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,正如我上面的路由中所定义的那样。我是否搞砸了我的路线,还是我期望这种情况发生是正确的?
【问题讨论】:
-
第一次 FileExists 调用发生在控制器运行之前,并且必须返回 false 否则 IIS 将尝试将其作为静态文件提供服务。对实际 aspx 文件的请求稍后会在控制器请求视图时出现。
标签: asp.net asp.net-mvc virtualpathprovider