Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore controller route attribute for a specific action [duplicate]

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() {}

        ...
    }
like image 520
William Avatar asked Oct 14 '19 14:10

William


1 Answers

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.

like image 144
Mortaza Ghahremani Avatar answered Nov 15 '22 01:11

Mortaza Ghahremani