8

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

        ...
    }
0

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.