I've got many actions on my controller and I want to use a specific route only for one action if possible. I'm looking for a way to override the route defined at the controller level.
[Route("api/candidate/attached-file")]
public class AttachedFileController : Controller
{
//overriding controller route
[HttpPost("api/candidate/{id}/attached-file")]
public async Task<IActionResult> AttachFile(string id, [FromBody] AttachedFile file)
{
...
}
//using controller route
[HttpGet("{id}")]
public ActionResult Get(string id) {}
[HttpGet]
public ActionResult GetAll() {}
...
}
For ignoring the route method of controller use like below:
[Route("/api/customControllerName/getCustomMethodName")]
public ActionResult GetCustomAll() {
}
using [Route("/")] ignores the api route addressings.
[Route("api/candidate/attached-file")]
public class AttachedFileController : Controller
{
//overriding controller route
[HttpPost("api/candidate/{id}/attached-file")]
public async Task<IActionResult> AttachFile(string id, [FromBody] AttachedFile file)
{
...
}
//using controller route
[HttpGet("{id}")]
public ActionResult Get(string id) {}
[Route("/api/test/value")]
public ActionResult GetCustomAll() {
}
}
So,By calling api/test/value address GetCustomAll() method will be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With