6

Is there any way to add a parameter to the Controller Routing attribute?

something like:

[Route("controller/{id}/"]

public class Controller {
   public Controller(string id) { /*..*/ }

   [HttpGet]
   public ActionResult Get() { /*..*/ }
}
3
  • The route parameter is passed into each action method, not the controller's constructor. Commented Aug 1, 2019 at 13:15
  • What exactly are you trying to achieve? Commented Aug 1, 2019 at 13:28
  • Trying to add a common parameter to all routes on an existing controller Commented Aug 2, 2019 at 14:08

1 Answer 1

4

The [FromRoute] attribute on your controller method arguments should get you what you need.

Example

[Route("api/Test/{testId}")]
public class TestController: ControllerBase 
{
    [HttpGet]
    [Route("echo")]
    public void TestMethod([FromRoute]string testId)
    {
        return testId;
    }

}

Also see this S.O. for more information.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.