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

.net – 在F#Web Api项目中使用可选的查询参数,第1张

概述我正在使用F#ASP.NET模板将C#webapi项目转换为F#.除了可选的查询参数,一切都很好.我不断得到这个错误 { "message": "The request is invalid.", "messageDetail": "The parameters dictionary contains an invalid entry for parameter 'start' f 我正在使用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提取控制器 *** 作using ParameterInfo的默认值.显然,F#编译器不会像C#一样编译默认值(即使使用DefaultParameterValueAttribute)

什么是最好的方式或解决这个问题?是否需要注入或实现自己的ParameterBinding的一些过滤器?

解决方法@H_404_21@ 您可以使用自定义ActionFilterattribute实现解决此问题.以下代码支持在缺少 *** 作参数时使用DefaultParameterValue值,以及当action参数具有System.Reflection.Missing类型的值时.

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

namespace System.Web.httpopen System.Reflectionopen System.Web.http.Filtersopen System.Web.http.Controllersopen 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项目中使用可选的查询参数全部内容,希望文章能够帮你解决.net – 在F#Web Api项目中使用可选的查询参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1098913.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存