Pass decimal as value in WebAPI 2 URL

samneric picture samneric · Feb 21, 2014 · Viewed 10.9k times · Source

I am creating a Web Api (v2.0) Method that needs to take in a decimal value as its parameter.

I am getting a 404 not found error if I use the following URL:

http://localhost:4627/api/Product/Eligibility/10.5

But it works if I use the following URL against an Int parameter:

Http://localhost:4627/api/Product/Eligibility/10

These are the two corresponding Methods in the api:

// GET api/Product/Eligibility/10.0
[Route("api/Product/Eligibility/{amount:decimal}")]
public decimal GetEligibiilty(decimal amount)
{
    return amount;
}

// GET api/Product/Eligibility/10
[Route("api/Product/Eligibility/{amount:int}")]
public decimal GetEligibiilty(int amount)
{
    return amount;
}

Steve

Answer

samneric picture samneric · Feb 21, 2014

Got it working by adding a "/" to the end of the URL!

http://localhost:4627/api/Product/Eligibility/10.5/

Will find this method:

// GET api/Product/Eligibility/10.5/
[Route("api/Product/Eligibility/{amount:decimal}/")]
public decimal GetEligibiilty(decimal amount)
{
    return amount;
}

Steve