3

Good Morning,

I’m having difficulty setting up my HTTPGETs and then testing the solution in Postman.

I’m trying to return a single result on both occasions however when I input the parameters nothing loads. So I'm clearly missing something which i need some help on please.

I have 1 parameter {id} in my CashMovementController and if I navigate to localhost/api/cashmovements/{id} it loads however if pass the {id} parameter in postman it fails.

Then in my BondCreditRatingsController I have 2 parameters {ISIN} & {Date} and again I'm not sure how to approach this.

Love to hear some advice/help on this please

Thanks GWS

Startup.cs

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

CashMovementsController.cs

[Route("api/[controller]")]
public class CashMovementsController : Controller
{
    private ICashMovementRepository _cashmovementRepository;

    [HttpGet("{id}", Name = "GetCashMovement")]
    public IActionResult Get(int id)
    {

        CashMovement _cashmovement = _cashmovementRepository.GetSingle(u => u.CashMovementId == id);

        if (_cashmovement != null)
        {
            CashMovementViewModel _cashmovementVM = Mapper.Map<CashMovement, CashMovementViewModel>(_cashmovement);
            return new OkObjectResult(_cashmovementVM);
        }
        else
        {
            return NotFound();
        }
    }
}

BondCreditRatingsController.cs

[Route("api/[controller]")]
public class BondCreditRatingsController : Controller
{
    private IBondCreditRatingRepository _bondcreditratingRepository;


    public BondCreditRatingsController(IBondCreditRatingRepository bondcreditratingRepository)
    {
        _bondcreditratingRepository = bondcreditratingRepository;
    }

    [HttpGet("{id}", Name = "GetBondCreditRating")]
    public IActionResult Get(string id, DateTime efffectivedate)
    {

        BondCreditRating _bondcreditrating = _bondcreditratingRepository.GetSingle(u => u.ISIN == id, u => u.EffectiveDate == efffectivedate);

        if (_bondcreditrating != null)
        {
            BondCreditRatingViewModel _bondcreditratingVM = Mapper.Map<BondCreditRating, BondCreditRatingViewModel>(_bondcreditrating);
            return new OkObjectResult(_bondcreditratingVM);
        }
        else
        {
            return NotFound();
        }
    }
Glenn Sampson
  • 1,188
  • 3
  • 12
  • 30
  • What's error message in Postman? Something about route? – Huske Sep 22 '16 at 05:56
  • Hello, I dont have an error in Postman for the 1 parameter {id} however the page is not single its all my data ie http://localhost:5000/api/cashmovements/?id=646 is really just a get all – Glenn Sampson Sep 22 '16 at 06:29
  • have you tried calling it like localhost:5000/api/cashmovements/646? You have a route defined so that should pass the proper parameter. – Huske Sep 22 '16 at 06:32
  • Yes i have and that works as expected, will this be overriding Postman? – Glenn Sampson Sep 22 '16 at 06:36
  • @Glenn_Sampson, what do you mean overriding Postman? – Huske Sep 22 '16 at 06:37
  • I had expected that when i entered a param {id} and {value} in postman then sent the request a single result would be returned – Glenn Sampson Sep 22 '16 at 06:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123908/discussion-between-glenn-sampson-and-husein-roncevic). – Glenn Sampson Sep 22 '16 at 06:41

1 Answers1

4

If you want to map it to api/Controller/method/id you would need to use the code below because you want to map parameter order (no other identifier) to a specific parameter name in the action.

[HttpGet("GetCashMovement/{id}")]

Your current code should work with below since you are using named parameters and because the request can't be mapped to any other template.

/api/CashMovements/GetCashMovement?id=1

But that attribute syntax will also (possibly unintentionally) trigger:

/api/CashMovements/1

Since a sum of your defined template for that action is:

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

Reason to why /api/ApiTest/GetCashMovement maps GetCashMovement.Get(int i) is because id is defined as optional in startup

routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/**{id?}**");

A question mark (?) after the route parameter name defines an optional parameter.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.0#create-routes

burned4
  • 51
  • 3