Skip to content

Commit 031a9c8

Browse files
authored
Add method validation breaking change (#26221)
1 parent abf4d3a commit 031a9c8

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

docs/core/compatibility/6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff
3636
| [Razor: Logging ID changes](aspnet-core/6.0/razor-pages-logging-ids.md) | RC1 |
3737
| [Razor: RazorEngine APIs marked obsolete](aspnet-core/6.0/razor-engine-apis-obsolete.md) | Preview 1 |
3838
| [SignalR: Java Client updated to RxJava3](aspnet-core/6.0/signalr-java-client-updated.md) | Preview 4 |
39+
| [TryParse and BindAsync methods are validated](aspnet-core/6.0/tryparse-bindasync-validation.md) | RC 2 |
3940

4041
## Core .NET libraries
4142

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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()`

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ items:
6565
href: aspnet-core/6.0/razor-engine-apis-obsolete.md
6666
- name: "SignalR: Java Client updated to RxJava3"
6767
href: aspnet-core/6.0/signalr-java-client-updated.md
68+
- name: TryParse and BindAsync methods are validated
69+
href: aspnet-core/6.0/tryparse-bindasync-validation.md
6870
- name: Core .NET libraries
6971
items:
7072
- name: API obsoletions with non-default diagnostic IDs
@@ -475,6 +477,8 @@ items:
475477
href: aspnet-core/6.0/razor-engine-apis-obsolete.md
476478
- name: "SignalR: Java Client updated to RxJava3"
477479
href: aspnet-core/6.0/signalr-java-client-updated.md
480+
- name: TryParse and BindAsync methods are validated
481+
href: aspnet-core/6.0/tryparse-bindasync-validation.md
478482
- name: .NET 5
479483
items:
480484
- name: ASP.NET Core apps deserialize quoted numbers

0 commit comments

Comments
 (0)