Search code examples
.netasp.net-coreasp.net-core-webapiasp.net-web-api-routing

Web API "Nested" Route?


No real code here, but looking for direction. I am using .net core web api. A potential requirement came up to use somewhat of a nested route for the api that is based on the user.

ie: https://blah.com/controller/YourNameHere/variable

Is that even possible?

I am used to this format in a controller:

[ApiController]
[Route("api/[controller]")]

I also believe you can specify static route like /api/[controller]/somethingstatic/id for example.


Solution

  • You can specify routes on the ControllerMethods you use by specifying it with the HttpGet prefix: Eg:

    [HttpGet("StageResult/{name}/something/{id}")]
    public ActionResult<IEnumerable<TeamResultsDTO>> GetStageResults(string name, int id){ ... 
    
    

    It is explained more clearly here: How to pass multiple parameters to a get method in ASP.NET Core