Pass decimal as value in WebAPI 2 URL

时间秒杀一切 提交于 2021-02-07 04:57:17

问题


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


回答1:


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




回答2:


I recently had this problem while working with Web API in Visual Studio 2015. I solved the problem by adding ?parameterName=decimalValue at the end of the URL. In your case could be something similar to this:

http://localhost:4627/api/Product/Eligibility?amount=10.5

I hope can help.




回答3:


Had a similar problem with passing a decimal in the URL, anyone using ASP.NET web pages here is a solution when redirecting after a transaction and the variable amount has a decimal in it:

Response.Redirect(@Href("~/Thankyou", amount + "/"));

The slash at the end of the URL is required to get it working.



来源:https://stackoverflow.com/questions/21944225/pass-decimal-as-value-in-webapi-2-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!