`[FromRoute]` does not work when used as model property, not as action parameter.
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
[FromRoute] does not work when used as model property, not as action parameter (see reproduction).
-
Value for the property is set from the body.

-
Property generates validation error (“field is required”), if omitted on request (but route value is specified).

-
Swagger generates incorrect body schema for the model - [FromRoute] propery shown as a part of the body schema.

Expected Behavior
- Parameter is mapped from route, not from the body.
- Parameter does not generate 400 Bad Request result with model validation error, when parameter is missing in body.
- Swagger gen schema generated properly - route parameter is not part of the body schema.
Steps To Reproduce
dotnet new webapi- Create new
.csfile and paste this code:
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
namespace Controllers;
[ApiController, Route("/Companies/{companyId}/[controller]")]
public class EmployeesController : ControllerBase
{
[HttpPost]
// [HttpPut] // Same result
// [HttpPatch] // Same result
public IActionResult CreateEmployee(CreateEmployeeDto dto)
{
return Ok(dto);
}
}
public record CreateEmployeeDto
{
[FromRoute]
// [FromRoute(Name = "companyId")] // Same result
public string CompanyId { get; init; } = null!;
// [FromBody] // Same result
// [FromForm] // Same result
[Range(1, int.MaxValue)]
public int PhoneNumber { get; init; }
}
- Open swagger and test the action. Use different values for “companyId” for route value and for body parameter (to distinguish them).

Program.cs is untouched. You may remove WeatherController as you wish.
Exceptions (if any)
No response
.NET Version
6.0.403, 7.0.100
Anything else?
VS Code.
.NET SDK:
Version: 7.0.100
Commit: e12b7af219
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19044
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.100\
Host:
Version: 7.0.0
Architecture: x64
Commit: d099f075e4
.NET SDKs installed:
6.0.403 [C:\Program Files\dotnet\sdk]
7.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
Environment variables:
Not set
global.json file:
Not found
Issue Analytics
- State:
- Created 10 months ago
- Comments:24 (18 by maintainers)
Top Results From Across the Web
FromRoute does not set Url parameters in separate DTO ...
Since this is a web api project, the [ApiController] attribute applies inference rules for the default data sources of action parameters.
Read more >How to bind [FromRoute] and [FromBody] into one model in ...
Example for [FromBody] model binding. This works fine and is self-explaining. But there are cases where you will want both: A combination of ......
Read more >Model Binding in ASP.NET Core
Learn how model binding in ASP.NET Core works and how to customize its behavior.
Read more >FromQuery? FromForm? What do the .NET attributes do?
The FromRoute is used if a route attribute is used as part of the action. By adding FromRoute to the action's parameter, it...
Read more >How to use parameter binding in minimal APIs in ASP.NET ...
The goal of this post is to give you a head start on working with parameter binding in minimal APIs. To use the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thank you, I will wait for a fix 😃 Do I need to provide any additional feedback / use case scenarios?
Closing since we are tracking the issue here #46140