gpt4 book ai didi

使用记录类型的 F# WebApi 未提供所需的 XML 输出

转载 作者:行者123 更新时间:2023-12-03 20:27:21 25 4
gpt4 key购买 nike

工作代码在这个问题的底部!

在 F# 中使用以下 WebApi Controller :

namespace FsWeb.Controllers

open System.Web
open System.Web.Mvc
open System.Net.Http
open System.Web.Http
open Microsoft.FSharp.Linq;
open Microsoft.FSharp.Data.TypeProviders;

type dbSchema = SqlDataConnection<"conn string">

[<CLIMutable>]
type ViewModelRec = { CustomerId : string }

type ValuesController() =
inherit ApiController()
let db = dbSchema.GetDataContext()
do
db.DataContext.Log <- System.Console.Out

// GET /api/values
member x.Get(take:int, skip:int) =
let t = take
let s = skip

query {
for row in db.Reservations do
skip s
take t
select { CustomerId = row.CustomerId.ToString() }
}

XML 输出为:

<ArrayOfViewModelRec xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/FsWeb.Controllers">
<ViewModelRec>
<CustomerId_x0040_>A98971F0-5F93-4F0D-B5AC-95E161458381</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>60DF7CF6-91FB-4BBE-AD6B-FE0CAA32F60E</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>84506E40-139C-4DD1-B396-3CE13DC20996</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>D9FABC12-9B19-46C2-9776-E31171A302D5</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>B1B6C617-AA0E-46A9-A0B6-312A229BE178</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>BAB194AE-CD2C-400F-B81E-2A293C50C404</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>AE001AB4-8410-43BF-9C93-AE5A2EACFDF5</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>2DDBDB91-9307-4BF7-865F-C6237D09D542</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>A3E93E5C-2B7E-4B8D-8AAD-AD245E32093F</CustomerId_x0040_>
</ViewModelRec>
<ViewModelRec>
<CustomerId_x0040_>B64B814F-9B4C-46BD-A804-648309FA7DD0</CustomerId_x0040_>
</ViewModelRec>
</ArrayOfViewModelRec>

当我改用一个类时,命名看起来是正确的(没有 x0040 标记)。

有人建议吗?

更新:解决方案:
namespace FsWeb.Controllers

open System.Web
open System.Web.Mvc
open System.Net.Http
open System.Web.Http
open System.Runtime.Serialization;
open Microsoft.FSharp.Linq;
open Microsoft.FSharp.Data.TypeProviders;

type dbSchema = SqlDataConnection<"here comes the connection string">

[<DataContract>]
type ViewModelRec = {
[<field: DataMember(Name="CustomerId")>]
CustomerId : string }

type ValuesController() =
inherit ApiController()
let db = dbSchema.GetDataContext()
do
db.DataContext.Log <- System.Console.Out

member x.Get(take:int, skip:int) =
let t = take
let s = skip

query {
for row in db.Reservations do
skip s
take t
select { CustomerId = row.CustomerId.ToString() }
}

最佳答案

我不是 F# 专家,但据我所知,记录类型属性 getter 由同名的内部字段支持,最后只有“@”。在您的情况下为“CustomerId@”。

然后,序列化程序选择序列化这些支持字段,而不是公共(public) getter,就像您拥有一个带有 [Serializable] 属性的 C# 类时一样。

它应该在您使用 DataContract/DataMember 时工作 - 请参阅 F# Serialization of Record types

关于使用记录类型的 F# WebApi 未提供所需的 XML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com