Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can register a model binder globally by implementing a ModelBinderProvider and inserting it into the list of services.</p> <p>Example use in Global.asax:</p> <pre><code>GlobalConfiguration.Configuration.Services.Insert(typeof(ModelBinderProvider), 0, new Core.Api.ModelBinders.DateTimeModelBinderProvider()); </code></pre> <p>Below is example code demonstrating a ModelBinderProvider and a ModelProvider implemenation that converts DateTime arguments in a culture aware manner (using the current threads culture);</p> <p>DateTimeModelBinderProvider implementation:</p> <pre><code>using System; using System.Web.Http; using System.Web.Http.ModelBinding; </code></pre> <p>...</p> <pre><code>public class DateTimeModelBinderProvider : ModelBinderProvider { readonly DateTimeModelBinder binder = new DateTimeModelBinder(); public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType) { if (DateTimeModelBinder.CanBindType(modelType)) { return binder; } return null; } } </code></pre> <p>DateTimeModelBinder implementation:</p> <pre><code>public class DateTimeModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { ValidateBindingContext(bindingContext); if (!bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName) || !CanBindType(bindingContext.ModelType)) { return false; } bindingContext.Model = bindingContext.ValueProvider .GetValue(bindingContext.ModelName) .ConvertTo(bindingContext.ModelType, Thread.CurrentThread.CurrentCulture); bindingContext.ValidationNode.ValidateAllProperties = true; return true; } private static void ValidateBindingContext(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } if (bindingContext.ModelMetadata == null) { throw new ArgumentException("ModelMetadata cannot be null", "bindingContext"); } } public static bool CanBindType(Type modelType) { return modelType == typeof(DateTime) || modelType == typeof(DateTime?); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload