Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET core HttpGet single Web API

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();
        }
    }
like image 941
Glenn Sampson Avatar asked Aug 28 '26 18:08

Glenn Sampson


1 Answers

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

like image 67
burned4 Avatar answered Aug 31 '26 12:08

burned4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!