|
| 1 | +--- |
| 2 | +title: "Breaking change: TryParse and BindAsync methods are validated" |
| 3 | +description: "Learn about the breaking change in ASP.NET Core 6.0 where `TryParse` and `BindAsync` methods on parameter types for `Map*` methods are validated at startup." |
| 4 | +ms.date: 09/22/2021 |
| 5 | +--- |
| 6 | +# TryParse and BindAsync methods are validated |
| 7 | + |
| 8 | +ASP.NET Core now validates `TryParse` and `BindAsync` methods on parameter types for `Map*` methods. If no valid method is found, ASP.NET Core looks for invalid methods and throws an exception at startup if one is found. The exception helps to avoid unexpected behavior by alerting you that your method signature may be incorrect. |
| 9 | + |
| 10 | +## Version introduced |
| 11 | + |
| 12 | +ASP.NET Core 6.0 RC 2 |
| 13 | + |
| 14 | +## Previous behavior |
| 15 | + |
| 16 | +In previous versions of ASP.NET Core 6, if a `TryParse` or `BindAsync` method has an invalid signature, no exception was thrown, and the framework tried to bind JSON from the body. |
| 17 | + |
| 18 | +```csharp |
| 19 | +// Todo.TryParse is not in a valid format. |
| 20 | +// Will try to bind from body as JSON instead. |
| 21 | +app.MapPost("/endpoint", (Todo todo) => todo.Item); |
| 22 | + |
| 23 | +public class Todo |
| 24 | +{ |
| 25 | + public string Item { get; set; } |
| 26 | + public static bool TryParse(string value) => true; |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## New behavior |
| 31 | + |
| 32 | +If ASP.NET Core finds a public `TryParse` or `BindAsync` method that doesn't match the expected syntax, an exception is thrown on startup. The previous example produces an error similar to: |
| 33 | + |
| 34 | +```txt |
| 35 | +TryParse method found on Todo with incorrect format. Must be a static method with format |
| 36 | +bool TryParse(string, IFormatProvider, out Todo) |
| 37 | +bool TryParse(string, out Todo) |
| 38 | +but found |
| 39 | +Boolean TryParse(System.String) |
| 40 | +``` |
| 41 | + |
| 42 | +## Type of breaking change |
| 43 | + |
| 44 | +This change can affect [binary compatibility](../../categories.md#binary-compatibility) and [source compatibility](../../categories.md#source-compatibility). |
| 45 | + |
| 46 | +## Reason for change |
| 47 | + |
| 48 | +This change was made so that developers are made aware of `BindAsync` and `TryParse` methods that have an invalid format. Previously, the framework would fall back to assuming the parameter is JSON from the body. This assumption can result in unexpected behavior. |
| 49 | + |
| 50 | +## Recommended action |
| 51 | + |
| 52 | +If your type has a `BindAsync` or `TryParse` method with different syntax for a reason other than parameter binding, you'll now encounter an exception at startup. To avoid this behavior, there are multiple strategies available: |
| 53 | + |
| 54 | +- Change your `BindAsync` or `TryParse` method to be `internal` or `private`. |
| 55 | +- Add a new `BindAsync` or `TryParse` method that has the syntax the framework looks for—invalid methods are ignored if a valid one is found. |
| 56 | +- Mark your parameter as `[FromBody]`. |
| 57 | + |
| 58 | +## Affected APIs |
| 59 | + |
| 60 | +- `RequestDelegateFactory.Create()` |
| 61 | +- All `IEndpointRouteBuilder.Map*()` methods, for example, `app.MapGet()` and `app.MapPost()` |
0 commit comments