gpt4 book ai didi

.net - 在 F# Web Api 项目中使用可选查询参数

转载 作者:行者123 更新时间:2023-12-03 17:53:16 27 4
gpt4 key购买 nike

我正在使用 F# ASP.NET 模板将 C# webapi 项目转换为 F#。除了可选的查询参数外,一切都很好。我不断收到此错误

{
"message": "The request is invalid.",
"messageDetail": "The parameters dictionary contains an invalid entry for parameter 'start' for method 'System.Threading.Tasks.Task`1[System.Net.Http.HttpResponseMessage] GetVendorFiles(Int32, System.Nullable`1[System.DateTime])' in 'Thor.WebApi.VendorFilesController'. The dictionary contains a value of type 'System.Reflection.Missing', but the parameter requires a value of type 'System.Nullable`1[System.DateTime]'."
}

F# 函数签名:
[<HttpGet; Route("")>]
member x.GetVendorFiles( [<Optional; DefaultParameterValue(100)>] count, [<Optional; DefaultParameterValue(null)>] start : Nullable<DateTime> ) =

C#函数签名:

[HttpGet]
[Route("")]
public async Task<HttpResponseMessage> GetVendorFiles(int count = 100,DateTime? start = null)

有谁知道任何解决方法?

更新:
我弄清楚了这个问题的原因。 ASP.NET 提取 Controller 操作的默认值 using ParameterInfo .显然 F# 编译器不像 C# 那样编译默认值(即使是 DefaultParameterValueAttribute )

什么是最好的方法或解决这个问题?是我需要注入(inject)或实现自己的过滤器吗 ParameterBinding ?

最佳答案

您可以使用自定义 ActionFilterAttribute 来解决此问题。执行。以下代码支持使用DefaultParameterValue缺少 Action 参数并且 Action 参数具有 System.Reflection.Missing 类型的值时的值.

代码也在 Gist ActionFilter for ASP.NET Web API DefaultParameterValue support in F# 中.

namespace System.Web.Http

open System.Reflection
open System.Web.Http.Filters
open System.Web.Http.Controllers
open System.Runtime.InteropServices

/// Responsible for populating missing action arguments from DefaultParameterValueAttribute values.
/// Created to handle this issue https://github.com/aspnet/Mvc/issues/1923
/// Note: This is for later version of System.Web.Http but could be back-ported.
type DefaultParameterValueFixupFilter() =
inherit ActionFilterAttribute()
/// Get list of (paramInfo, defValue) tuples for params where DefaultParameterValueAttribute is present.
let getDefParamVals (parameters:ParameterInfo array) =
[ for param in parameters do
let defParamValAttrs = param.GetCustomAttributes<DefaultParameterValueAttribute>() |> List.ofSeq
match defParamValAttrs with
// Review: we are ignoring null defaults. Is this correct?
| [x] -> if x.Value = null then () else yield param, x.Value
| [] -> ()
| _ -> failwith "Multiple DefaultParameterValueAttribute on param '%s'!" param.Name
]
/// Add action arg default values where specified in DefaultParameterValueAttribute attrs.
let addActionArgDefsFromDefParamValAttrs (context:HttpActionContext) =
match context.ActionDescriptor with
| :? ReflectedHttpActionDescriptor as ad ->
let defParamVals = getDefParamVals (ad.MethodInfo.GetParameters())
for (param, value) in defParamVals do
match context.ActionArguments.TryGetValue(param.Name) with
| true, :? System.Reflection.Missing
| false, _ ->
// Remove is null-op if key not found, so we handle both match cases OK.
let _ = context.ActionArguments.Remove(param.Name)
context.ActionArguments.Add(param.Name, value)
| _, _ -> ()
| _ -> ()
/// Override adding suport for DefaultParameterValueAttribute values.
override x.OnActionExecuting(context) =
addActionArgDefsFromDefParamValAttrs context
base.OnActionExecuting(context)
/// Override adding suport for DefaultParameterValueAttribute values.
override x.OnActionExecutingAsync(context, cancellationToken) =
addActionArgDefsFromDefParamValAttrs context
base.OnActionExecutingAsync(context, cancellationToken)

关于.net - 在 F# Web Api 项目中使用可选查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28160230/

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