Skip to content

Instantly share code, notes, and snippets.

@ashtonkj
Created September 9, 2014 13:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashtonkj/8152c3261c0ac3333148 to your computer and use it in GitHub Desktop.
Save ashtonkj/8152c3261c0ac3333148 to your computer and use it in GitHub Desktop.
WebApi Default Args Binder
type CustomBinder() =
interface IModelBinder with
member this.BindModel(actionContext:HttpActionContext, bindingContext :ModelBindingContext) =
let qs = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query)
bindingContext.Model <-
if (qs.AllKeys |> Seq.exists(fun q -> q.ToLower() = bindingContext.ModelName.ToLower())) then
qs.[bindingContext.ModelName] |> Some
else
None
true
type CustomBinderProvider() =
inherit ModelBinderProvider()
override this.GetBinder(configuration: System.Web.Http.HttpConfiguration , modelType: System.Type) =
CustomBinder() :> IModelBinder
type TestController() =
inherit ApiController()
[<HttpGetAttribute>]
member this.Test([<ModelBinderAttribute(typeof<CustomBinderProvider>)>]?argument: string) =
let arg = defaultArg argument "Hello"
arg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment