asp.net - ASP .NET MVC VirtualPathProvider

标签 asp.net asp.net-mvc asp.net-mvc-2 view virtualpathprovider

我正在编写一个 VirtualPathProvider 来动态加载我的 MVC View ,这些 View 位于不同的目录中。我在 MVC 之前成功拦截了调用(在 FileExists 中),但在 VirtualPathProvider 中,我得到了原始的预路由 url,如下所示:

~/Apps/Administration/Account/LogOn

就我个人而言,我知道 MVC 会寻找

~/Apps/Administration/Views/Account/LogOn.aspx

我应该从

读取文件内容
D:\SomeOtherNonWebRootDirectory\Apps\Administration\Views\Account\LogOn.aspx

但我不想对逻辑进行硬编码以“添加名为 Views 的目录并将 aspx 添加到末尾”。

此逻辑存储在哪里以及如何将其放入我的虚拟路径提供程序中?

谢谢。抱歉,如果我没说清楚。

最佳答案

已编辑

您需要创建一个继承WebFormViewEngine 并设置ViewLocationFormats 属性(继承自VirtualPathProviderViewEngine)的类。

默认值可以在MVC源代码中找到:

public WebFormViewEngine() {
    MasterLocationFormats = new[] {
        "~/Views/{1}/{0}.master",
        "~/Views/Shared/{0}.master"
    };

    AreaMasterLocationFormats = new[] {
        "~/Areas/{2}/Views/{1}/{0}.master",
        "~/Areas/{2}/Views/Shared/{0}.master",
    };

    ViewLocationFormats = new[] {
        "~/Views/{1}/{0}.aspx",
        "~/Views/{1}/{0}.ascx",
        "~/Views/Shared/{0}.aspx",
        "~/Views/Shared/{0}.ascx"
    };

    AreaViewLocationFormats = new[] {
        "~/Areas/{2}/Views/{1}/{0}.aspx",
        "~/Areas/{2}/Views/{1}/{0}.ascx",
        "~/Areas/{2}/Views/Shared/{0}.aspx",
        "~/Areas/{2}/Views/Shared/{0}.ascx",
    };

    PartialViewLocationFormats = ViewLocationFormats;
    AreaPartialViewLocationFormats = AreaViewLocationFormats;
}

然后,您应该清除 ViewEngines.Engines 集合并向其中添加您的 ViewEngine 实例。

关于asp.net - ASP .NET MVC VirtualPathProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3514965/

相关文章:

c# - 当 web.config 设置为 Forms 时,AuthenticationSection.Mode 返回 Windows

c# - 根据 Access 表检查 ID 并循环执行 SQL 语句

c# - 何时在 ASP.NET MVC 中实例化 EF4 上下文?

asp.net-mvc - ASP.NET MVC : Route with optional parameter,,但如果提供,则必须匹配\d+

asp.net-mvc - Asp.Net MVC Html Helper 扩展

javascript - 使用 Javascript 启用验证器

c# - 在列表项之间添加空间,或使项目更容易在 ASP.NET 下拉列表中的触摸屏上选择

jquery - 如何将日期选择器最大日期限制为今天?

javascript - 限制用户在文本区域中粘贴最多 (40) 个字符

entity-framework - 使用 MVC2 更新带有外键的 Entity Framework v4 对象失败