Возможно ли включить в AspnetCore иерархию маршрута контроллера (вложенные маршруты)?C#

Место общения программистов C#
Anonymous
 Возможно ли включить в AspnetCore иерархию маршрута контроллера (вложенные маршруты)?

Сообщение Anonymous »

(я создал этот вопрос, когда я уже нашел решение, так что я надеюсь, что это поможет другим людям столкнуться с этой проблемой. Тем не менее, все приветствуют критику или предложит лучший подход )
У меня есть родительский контроллер, как это:

[ApiController]
[Route("[controller]")]
public class ParentController() : ControllerBase
{
[HttpGet] public string Get() => $"Hellow, World!";
}
< /code>
и его ребенок: < /p>
[ApiController]
[Route("{parentId:int}/[controller]")]
public class ChildController() : ControllerBase
{
[HttpGet("{childId:int}")] public string Get(int parentId, int childId) => $"Hellow, World by child {childId} of parent {parentId}!";
}

I want to find a strict typed way to make the child controller reachable by route "/Parent/{parentId}/Child/{childId}"
the way like this is not a solution, because this route is not guaranteed by types' system and could be bring to issues during a refactoring:
[ApiController]
[Route("Parent/{parentId:int}/[controller]")]
public class ChildController() : ControllerBase


Подробнее здесь: https://stackoverflow.com/questions/794 ... -nested-ro

Вернуться в «C#»