Created
November 7, 2012 05:27
-
-
Save nakamura-to/4029706 to your computer and use it in GitHub Desktop.
ModelMetadata.ConvertEmptyStringToNull in ASP.NET Web API
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
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
config.Services.Replace(typeof(ModelMetadataProvider), new EmptyStringAllowedModelMetadataProvider()); | |
} | |
} | |
public class EmptyStringAllowedModelMetadataProvider : DataAnnotationsModelMetadataProvider | |
{ | |
protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor) | |
{ | |
var metadata = base.CreateMetadataFromPrototype(prototype, modelAccessor); | |
metadata.ConvertEmptyStringToNull = false; | |
return metadata; | |
} | |
protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable<Attribute> attributes, Type containerType, Type modelType, string propertyName) | |
{ | |
var metadata = base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName); | |
metadata.ConvertEmptyStringToNull = false; | |
return metadata; | |
} | |
} |
thanks mark
thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you