question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`[FromRoute]` does not work when used as model property, not as action parameter.

See original GitHub issue

Is 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).

  1. Value for the property is set from the body. image

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

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

Expected Behavior

  1. Parameter is mapped from route, not from the body.
  2. Parameter does not generate 400 Bad Request result with model validation error, when parameter is missing in body.
  3. Swagger gen schema generated properly - route parameter is not part of the body schema.

Steps To Reproduce

  1. dotnet new webapi
  2. Create new .cs file 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; }
}
  1. Open swagger and test the action. Use different values for “companyId” for route value and for body parameter (to distinguish them). image

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:closed
  • Created 10 months ago
  • Comments:24 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
tvarderocommented, Jan 25, 2023

Thank you, I will wait for a fix 😃 Do I need to provide any additional feedback / use case scenarios?

0reactions
brunolins16commented, Jan 26, 2023

Closing since we are tracking the issue here #46140

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found