Aspnetcore.docs: Need more detail on using TypeConverters in ASP.NET Core

Created on 4 Apr 2019  Â·  3Comments  Â·  Source: dotnet/AspNetCore.Docs

No examples are given on how to create a TypeConverter, and it is unclear how to write the TypeConverter to give an informative message when binding fails. E,g., if I had a custom "Date" type, and the provided value is "Bob", how should that be communicated from the TypeConverter implementation? In the custom modelbinder, you have access to ModelState, but not so in the TypeConverter.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P2 PU Source - Docs.ms doc-enhancement

Most helpful comment

Can dependency injection be used to register a TypeConverter, instead of the static TypeDescriptor.AddAttributes()?

https://stackoverflow.com/q/43378709

All 3 comments

Can dependency injection be used to register a TypeConverter, instead of the static TypeDescriptor.AddAttributes()?

https://stackoverflow.com/q/43378709

Request to PU: Provide a TypeConverter sample.

Per @dougbu in this comment in #17002

ASP.NET Core doesn't special-case DateTime values at all; binding is done using the default TypeConverter for the type. This is the same for most simple types though we add special cases for decimal, double, enum, and float. The details should already be described in the .NET and .NET Core docs.

If we don't already cover fact TypeConverters are used in model binding, that might be worth documenting somewhere so readers know where to look for supported formats. And, if we take the time to do that, might also mention the following special cases:

  • support thousands separators in received strings for decimal, double and float values unless FloatingPointTypeModelBinderProvider is removed from MvcOptions.ModelBinderProviders
  • disallow binding undefined integer values to an enum unless EnumTypeModelBinderProvider is removed from MvcOptions.ModelBinderProviders

/cc @pranavkm in case you have something to add

Moved to Master issue #16319

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Raghumu picture Raghumu  Â·  3Comments

nenmyx picture nenmyx  Â·  3Comments

fabich picture fabich  Â·  3Comments

aaron-bozit picture aaron-bozit  Â·  3Comments

danroth27 picture danroth27  Â·  3Comments