ASP.NET core HttpGet single Web API

2019-08-02 11:09发布

问题:

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();
        }
    }