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();
        }
    }
7
  • What's error message in Postman? Something about route? Commented Sep 22, 2016 at 5: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 localhost:5000/api/cashmovements/?id=646 is really just a get all Commented Sep 22, 2016 at 6:29
  • have you tried calling it like localhost:5000/api/cashmovements/646? You have a route defined so that should pass the proper parameter. Commented Sep 22, 2016 at 6:32
  • Yes i have and that works as expected, will this be overriding Postman? Commented Sep 22, 2016 at 6:36
  • @Glenn_Sampson, what do you mean overriding Postman? Commented Sep 22, 2016 at 6:37

1 Answer 1

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

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.