Skip to content

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

@tvardero

Description

@tvardero

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

Metadata

Metadata

Assignees

Labels

Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.feature-model-bindingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions