4

我现在正在尝试在 F# 中实现我自己的基本视图引擎。本质上我是从 VirtualPathProviderViewEngine 继承的。

为此,我需要设置两个视图位置,以便引擎知道在哪里查找视图。在我的 F# 类型中,我从上面继承并尝试将两个视图位置设置如下...

type FSharpViewEngine() =
inherit VirtualPathProviderViewEngine()

let viewLocations = [| "~/Views/{1}/{0}.fshtml"; "~/Views/Shared/{0}.fshtml" |]

member this.ViewLocationFormats = viewLocations
member this.PartialViewLocationFormats = viewLocations

上面的代码省略了 VirtualPathProviderViewEngine 所需的覆盖。我运行该项目,我收到一条错误消息说

属性“ViewLocationFormats”不能为 null 或为空。

我假设这意味着我没有正确设置上面的两个基本成员。我只是错误地分配了上述内容,还是您怀疑我做错了什么?

作为额外的信息,我在 Global.fs (global.asax) 的启动时添加了 ViewEngine,就像这样......

ViewEngines.Engines.Add(new FSharpViewEngine())
4

1 回答 1

7

If you just want to set properties of a base class, then you do not need member or override, but instead you need to use the assignment operator <- in the constructor. To implement the engine, you'll need to override two abstract methods that it defines, so you'll need something like this:

type FSharpViewEngine() =
    inherit VirtualPathProviderViewEngine() 

    let viewLocations = [| "~/Views/{1}/{0}.fshtml"; "~/Views/Shared/{0}.fshtml" |]
    do base.ViewLocationFormats <- viewLocations
       base.PartialViewLocationFormats <- viewLocations

    override x.CreatePartialView(ctx, path) = failwith "TODO!"
    override x.CreateView(ctx, viewPath, masterPath) = failwith "TODO!"
于 2012-11-06T12:37:47.837 回答