When I added /{index}
in Route, and int index
as a parameter (pic1) i got an error saying (pic2). I expected showed on (pic3). What should I do?
CodePudding user response:
Full example, since comments are already confusing ...
[ApiController]
[Route("api/[controller]")] // RouteAttribute needs to be on the class
public class MyController : ControllerBase // ControllerBase from Assembly Microsoft.AspNetCore.Mvc.Core version 6.0.0
{
[HttpGet("{id}")] // HttpGetAttribute for each route
public async Task<IActionResult> GetByIdAsync([FromRoute] int id)
{
// magic faery dust
}
}
example for GET: https://localhost:5001/api/mycontroller/42
CodePudding user response:
Use [] instead of {} I mean :
[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class NameController
{
[HttpGet("get")]
public ActionResult Get(int index)
{
...
}
}