locked
how to assign a name to an action method within the same controller RRS feed

  • Question

  • User379720387 posted

    The highlighted area shows how all my controller action methods are set up, now I need to add a name so it can be referenced from within the same controller. Examples I have seen modify [HttpGet] but then it triggers a non -unique exception for the method name.

    Is there a way to do this without leaving my exisitng approach intact?

    If not, what changes do I need to make so this action method has a name?

    [Route("ByServiceSingle/{id}")]
    [HttpGet]
            public async Task<ActionResult<List<ServiceSingle>>> GetServiceSingle(int id) => await context.Services
                blah blah
                .ToListAsync();

    Monday, May 3, 2021 1:55 AM

Answers

  • User503812343 posted

    the following code will use the action method name automatically as a route, no need to

    [Route("[action]")]

    if you need different name than action method name then you can use name property with HTTPGET

     [HttpGet("{id}", Name = "ByServiceSingle")]

    for more details action method

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 3, 2021 8:20 AM