-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding support for TryParse
as a way to bind primitives
#39682
Comments
Thanks for contacting us. We're moving this issue to the |
|
I have the same opinion, but I prefer to capture the possibility here for discussion if we should rewrite or reuse. I saw that was decided rewrite most of the code for minimal and probably the same will happen here. |
I think introduce the An alternative option is creating a new internal property to the |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API Review: We could add a new |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API review: Approved with notes:
+ public sealed class TryParseModelBinderProvider : IModelBinderProvider
+ {
+ public IModelBinder? GetBinder(ModelBinderProviderContext context);
+ } |
The following update was needed during the implementation and need to be reviewed: namespace Microsoft.AspNetCore.Mvc;
public class MvcOptions : IEnumerable<ICompatibilitySwitch>
{
+ /// <summary>
+ /// Gets or sets the flag that determines if binding is disabled for Parseable types (contains a TryParse method)
+ /// and will treat them as <see cref="ModelMetadata.IsComplexType"/>, in case,
+ /// there isn't a <see cref="System.ComponentModel.TypeConverter"/> from <see cref="string"/> registered.
+ /// </summary>
+ public bool SuppressParseableTypesBinding { get; set; }
} |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API feedback: We are going to punt on the options change until we get more feedback. Users should be able to workaround this by using an explicit |
Current design
Types that do not contain a
Converter
fromstring
available are consideredComplex Types
.aspnetcore/src/Mvc/Mvc.Abstractions/src/ModelBinding/ModelMetadata.cs
Line 609 in 85ac505
That means they will be bind using the
ComplexObjectModelBinder
when they are not special types that have your own binder (Eg.CancellationToken
orDictionary
) or when the[FromBody]
was not inferred, in this case theBodyModelBinder
will be used.On the other hand, when it is detected as a simple type (can be converted from string) the
SimpleTypeModelBinder
will be used, except for the types listed below:DateTimeModelBinder
=>context.Metadata.UnderlyingOrModelType == typeof(DateTime)
DecimalModelBinder
=>modelType == typeof(decimal)
DoubleModelBinder
=>modelType == typeof(double)
FloatModelBinder
=>modelType == typeof(float)
EnumTypeModelBinder
=>context.Metadata.IsEnum
Today, this binder will basically use the actual value if the model type is
string
or call the converter when not.Proposed Change
There is already the class
Microsoft.AspNetCore.Http.ParameterBindingMethodCache
exposing two methodsHasTryParseMethod
andFindTryParseMethod
that will be used in this proposal, however, currently it works only withInvariantCulture
instead of allowing the caller to provide a differentCulture
what is required in this proposal and will require a small change to this class.With the capability to detect the
TryParse
method, the proposal is changeModelMetadata
to include a newinternal
propertyHasTryParse
, that will indicate that aTryParse
method is available with the type.An important aspect of this change, the
ModelMetadata
will hold a static instance of theMicrosoft.AspNetCore.Http.ParameterBindingMethodCache
that will be used across all calls.With this new property available the proposal is to add a new public available
ModelBinderProvider
that will create aModelBinder
only whenModelMetadata.HasTryParse == true
This new provider will be added to the list of the default ModelProviders and executed before the
SimpleTypeModelBinderProvider
. That means that for all types that contains aTryParse
and was processed previously by theSimpleTypeModelBinderProvider
because it has aConverter
fromstring
will now be bound using the newTryParseModelBinder
. A very common example will beint
type.Also, this proposal will not change how types that have a
TryParse
method but not aConverter
fromstring
have their Binding Source inferred, so, for those types of the parameter will need to be explicitly defined the binding source, eg:FromQuery
.Usage Examples
The new behavior will be enabled without any code change, however, the following piece of code will rollback to the previous behavior if the users want to.
The text was updated successfully, but these errors were encountered: