Created
September 9, 2014 13:59
-
-
Save ashtonkj/8152c3261c0ac3333148 to your computer and use it in GitHub Desktop.
WebApi Default Args Binder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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