Look for best practices for working with nested routes in .NET Core MVC.
Let's say CampusController.cs works with the base model:
[Route("api/campus/")] public class CampusController : Controller { ... [HttpGet] [Route("{campusId}")] public IActionResult GetCampusInfo ([FromQuery]int campusId) { ... } }
And BuildingController.cs works with the child model:
[Route("api/campus/{campusId}/building")] public class BuildingController : Controller { ... [HttpGet] [Route("{buildingId}")] public IActionResult GetBuilding ([FromQuery]int buildingId) { ... } [Route("{buildingId}/")] public IActionResult GetBuilding ([FromQuery]int buildingId) { ... } .... (more Action Methods) }
If the buildingId mapped directly to the database, it can be restored even if the provided campusId not a parent. To clear the url when calling /api/campus/{campusId}/building/{buildingId} , I would check {campusId} and return a 4xx encoded IActionResult if it is not valid. There should be a better way than incorporating validation logic into every action method inside the BuildingController .
- Is there a way to cascade multiple Action methods on different controllers? So that the check method on
CampusController called first and in turn call the method on BuildingController ? - Is there a way to test at the
campusId controller campusId at the controller level, which can short circuit and return an ActionResult if the test is not completed?
EDIT: when I refer to the validation logic, I mean the API signals; not the business logic that actually determines if campusId / is invalid.
Thanks in advance!
source share