Attribute-based routing, arguments with different name to variable - aspnetcore

Viewed 827

I need to do this

[Route("/api/highfive/{person_name}")]
public IActionResult HighFive(string personName){
   //do stuff
}

Is there a way to map variable personName to what is supplied as person_name from the route?

The two below options work -as long as there is a match between route argument and variable name (which makes sense).

[Route("/api/highfive/{person_name}")]
public IActionResult HighFive(string person_name){
   //do stuff
}

[Route("/api/highfive/{personName}")]
public IActionResult HighFive(string personName){
   //do stuff
}

But why do you want this??

  • Swagger generates documentation that has to match that of a previous API, this is replacing - and it uses the variable names from route to generate documentation.

  • using person_name as a variable name is against our naming conventions

  • Yes it is kind of silly - but if there is an easy fix I am not aware of, that would be awesome.

1 Answers

The binding source attributes allow for configuration of the name used for model-binding. In your example, you're getting the value from the route, which means you can use the FromRoute attribute on the parameter:

[FromRoute(Name="person_name")] string personName
Related