在F#中设置基本成员值

Dom*_*Dom 4 asp.net-mvc f#

我现在正试图在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
Run Code Online (Sandbox Code Playgroud)

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

属性"ViewLocationFormats"不能为null或为空.

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

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

ViewEngines.Engines.Add(new FSharpViewEngine())
Run Code Online (Sandbox Code Playgroud)

Tom*_*cek 7

如果您只想设置基类的属性,那么您不需要memberoverride,而是需要<-在构造函数中使用赋值运算符.要实现引擎,您需要覆盖它定义的两个抽象方法,因此您需要这样的东西:

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!"
Run Code Online (Sandbox Code Playgroud)