-
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
New TryParseModelBinder #40233
New TryParseModelBinder #40233
Conversation
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinderProvider.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Metadata/DefaultBindingMetadataProvider.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/test/ModelBinding/Metadata/DefaultModelMetadataTest.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Brennan <brecon@microsoft.com>
…ns16/aspnetcore into brunolins16/issues/39682
src/Mvc/Mvc.Core/test/ModelBinding/Metadata/DefaultModelMetadataTest.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for driving this PR!
} | ||
catch (InvalidOperationException) | ||
{ | ||
// The ParameterBindingMethodCache.FindTryParseMethod throws an exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion - we could update FindTryParseMethod
to not throw via an additional parameter. Not catching exceptions is certainly a lot nicer since it avoids things like first-chance exceptions in VS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I will open a new PR just for that, ok?
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
Expression.Block( | ||
Expression.Assign(modelValue, Expression.Convert(parsedValue, modelValue.Type)), | ||
Expression.Assign(BindingResultExpression, Expression.Call(SuccessBindingResultMethod, modelValue))), | ||
Expression.Call(AddModelErrorMethod, BindingContextExpression, Expression.Constant(new FormatException()))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the format exception use https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Abstractions/src/ModelBinding/Metadata/ModelBindingMessageProvider.cs#L41-L47 as the message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is exactly what the TryAddModelError will do:
if (exception is FormatException || exception is OverflowException) |
src/Mvc/Mvc.Core/src/ModelBinding/Metadata/DefaultBindingMetadataProvider.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Pranav K <prkrishn@hotmail.com>
…ns16/aspnetcore into brunolins16/issues/39682
Fixes #39682