'How do I use a router parameter with dash (-) in .NET 6?
In the code below, how do I assign the app-name value to ApplicationName?
[ApiController]
[Route("testcontroller/app-name")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules(string ApplicationName)
{
throw new NotImplementedException($"/app/{ApplicationName}/modules not implemented");
}
}
Solution 1:[1]
By convention (no dash)...
[ApiController]
[Route("testcontroller/{appName}")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules([FromRoute]string appName)
{
throw new NotImplementedException($"/app/{appName}/modules not implemented");
}
}
Or by using the Name property on the FromRoute attribute:
[ApiController]
[Route("testcontroller/{app-name}")]
public class TestController : ControllerBase
{
[HttpGet(Name = "modules")]
public IActionResult GetModules([FromRoute(Name = "app-name")]string appName)
{
throw new NotImplementedException($"/app/{appName}/modules not implemented");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
