Home > Enterprise >  How do I use a router parameter with dash (-) in .NET 6?
How do I use a router parameter with dash (-) in .NET 6?

Time:04-07

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");
    }
}

CodePudding user response:

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");
    }
}
  • Related