本文介绍了在WebAPI 2 URL中将小数作为值传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个Web Api(v2.0)方法,该方法需要使用一个十进制值作为参数.
I am creating a Web Api (v2.0) Method that needs to take in a decimal value as its parameter.
如果使用以下URL,则会收到404 not found错误:
I am getting a 404 not found error if I use the following URL:
http://localhost:4627/api/Product/Eligibility/10.5
但是如果我对Int参数使用以下URL,它会起作用:
But it works if I use the following URL against an Int parameter:
Http://localhost:4627/api/Product/Eligibility/10
这些是api中两个对应的方法:
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;
}
史蒂夫
推荐答案
在URL末尾添加"/"
使其正常工作!
Got it working by adding a "/"
to the end of the URL!
http://localhost:4627/api/Product/Eligibility/10.5/
将找到此方法:
// GET api/Product/Eligibility/10.5/
[Route("api/Product/Eligibility/{amount:decimal}/")]
public decimal GetEligibiilty(decimal amount)
{
return amount;
}
史蒂夫
这篇关于在WebAPI 2 URL中将小数作为值传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!