dotnet / dotnet/aspnetcore

CancellationToken property in ApiController model breaks model binding by default

Open
#57,392 7 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Found a very interesting issue when migrating from .NET 7 to .NET 8.

We have a base filter model with a property:

public CancellationToken CancellationToken { get; set; }
It was marked as [JsonIgnore] and used to easily share the CancellationToken from top to bottom.

I noticed that the model doesn't automatically bind in some of the POST requests (it's an API Controller). It doesn't work with Newtonsoft.Json or System.Text.Json. However, with the [FromBody] attribute, it works. Also the request body is perfect.

Finally, I found that if the model has a property of type CancellationToken, it doesn't work. But since I converted the property into a field with Get/Set methods, it works perfectly.

Pretty funny behavior, in .NET 7 and earlier, it was working fine.

### Expected Behavior

_No response_

### Steps To Reproduce

just add to api request model property with Type CancellationToken

### Exceptions (if any)

_No response_

### .NET Version

.net 8

### Anything else?

ASP.NET Core version: 8
The IDE: VS 2022

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.