C# 如何创建一个webapi方法,该方法以2个double和一个timespan作为参数?
我正在使用ASP Webapi2.net项目 我正在尝试编写一个控制器,它将纬度、经度和时间跨度作为参数,然后返回一些JSON数据。(有点像商店定位器) 我有以下控制器代码C# 如何创建一个webapi方法,该方法以2个double和一个timespan作为参数?,c#,asp.net,asp.net-web-api,asp.net-web-api2,asp.net-web-api-routing,C#,Asp.net,Asp.net Web Api,Asp.net Web Api2,Asp.net Web Api Routing,我正在使用ASP Webapi2.net项目 我正在尝试编写一个控制器,它将纬度、经度和时间跨度作为参数,然后返回一些JSON数据。(有点像商店定位器) 我有以下控制器代码 public dynamic Get(decimal lat, decimal lon) { return new string[] { lat.ToString(), lon.ToString()}; } 我在WebAPIConfig.cs类的顶部放了下面一行 c
public dynamic Get(decimal lat, decimal lon)
{
return new string[] { lat.ToString(), lon.ToString()};
}
我在WebAPIConfig.cs类的顶部放了下面一行
config.Routes.MapHttpRoute(
name: "locationfinder",
routeTemplate: "api/{controller}/{lat:decimal}/{lon:decimal}"
);
当我做下面的调用时,我得到一个404错误
http://localhost:51590/api/MassTimes/0.11/0.22
我可以在查询字符串中使用小数吗?我该怎么做 你有没有考虑过偶然调查一下?URL涵盖了细节,但是,特别是在构建API时,属性路由确实简化了事情,并允许在路由中轻松开发模式 如果你最终选择了这条路线(ha),那么你可以这样做:
[RoutePrefix("api/masstimes")]
public class MassTimesController : ApiController
{
[HttpGet]
[Route("{lat}/{lon}")]
public ICollection<string> SomeMethod(double lat, double lon, [FromUri] TimeSpan time)
{
string[] mylist = { lat.ToString(), lon.ToString(), time.ToString() };
return new List<string>(myList);
}
}
[RoutePrefix(“api/masstimes”)]
公共类MassTimeController:ApiController
{
[HttpGet]
[路由(“{lat}/{lon}”)]
公共ICollection SomeMethod(双lat、双lon、[FromUri]时间跨度时间)
{
字符串[]mylist={lat.ToString(),lon.ToString(),time.ToString()};
返回新列表(myList);
}
}
现在,您可以通过调用gethttp://www.example.net/api/masstimes/0.00/0.00?time=00:01:10
本文还介绍了其他可用选项(例如,
[FromBody]
和其他),您可能会发现这些选项很有用。您是否考虑过偶尔研究一下?URL涵盖了细节,但是,特别是在构建API时,属性路由确实简化了事情,并允许在路由中轻松开发模式
如果你最终选择了这条路线(ha),那么你可以这样做:
[RoutePrefix("api/masstimes")]
public class MassTimesController : ApiController
{
[HttpGet]
[Route("{lat}/{lon}")]
public ICollection<string> SomeMethod(double lat, double lon, [FromUri] TimeSpan time)
{
string[] mylist = { lat.ToString(), lon.ToString(), time.ToString() };
return new List<string>(myList);
}
}
[RoutePrefix(“api/masstimes”)]
公共类MassTimeController:ApiController
{
[HttpGet]
[路由(“{lat}/{lon}”)]
公共ICollection SomeMethod(双lat、双lon、[FromUri]时间跨度时间)
{
字符串[]mylist={lat.ToString(),lon.ToString(),time.ToString()};
返回新列表(myList);
}
}
现在,您可以通过调用gethttp://www.example.net/api/masstimes/0.00/0.00?time=00:01:10
本文还介绍了您可能会发现有用的其他可用选项(例如[FromBody]
和其他选项)。几件事
首先,在路线的末尾添加一个尾随斜杠。参数绑定无法确定小数的结尾,除非您使用尾随斜杠强制它
http://localhost:62732/api/values/4.2/2.5/
第二,去掉路线声明上的类型:
config.Routes.MapHttpRoute(
name: "locationfinder",
routeTemplate: "api/{controller}/{lat}/{lon}"
);
第三,不要使用decimal
。使用双精度,因为它更适合描述纬度和经度坐标。
首先,在路线的末尾添加一个尾随斜杠。参数绑定无法确定小数的结尾,除非您使用尾随斜杠强制它
http://localhost:62732/api/values/4.2/2.5/
第二,去掉路线声明上的类型:
config.Routes.MapHttpRoute(
name: "locationfinder",
routeTemplate: "api/{controller}/{lat}/{lon}"
);
第三,不要使用
decimal
。改用double
,因为它更适合描述纬度和经度坐标。我建议使用double来表示纬度和经度。NET使用双精度,因此您将保持一致。更多信息请参见:时间跨度在哪里?我建议使用double表示纬度和经度。NET使用双精度,因此您将保持一致。在这里查看更多信息:TimeSpan
在哪里?你不是说GET吗http://www.example.net/api/masstimes/0.00/0.00?time=00:01:10
@rism我做了。固定的。谢谢你是说GEThttp://www.example.net/api/masstimes/0.00/0.00?time=00:01:10
@rism我做了。固定的。谢谢