Home > Enterprise >  How to add a parameter in GET and do not ger Internal Server Error?
How to add a parameter in GET and do not ger Internal Server Error?

Time:12-13

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?

pic1

pic2-1

pic2-2

pic3

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)
     {
     ...
     }
}
  • Related