C# 将url转换为查询字符串
因此,我正在尝试转换处理C# 将url转换为查询字符串,c#,asp.net,rest,C#,Asp.net,Rest,因此,我正在尝试转换处理POSTreqs的url: // this works http://localhost/api/locations/postlocation/16/555/556 到其对等查询字符串,该字符串应为: http://localhost/api/locations/postlocation?id=16&lat=88&lon=88 但是当我这样做的时候,我会犯这个错误。显然,它无法识别其中一个参数: "Message": "An error has occ
POST
reqs的url:
// this works
http://localhost/api/locations/postlocation/16/555/556
到其对等查询字符串,该字符串应为:
http://localhost/api/locations/postlocation?id=16&lat=88&lon=88
但是当我这样做的时候,我会犯这个错误。显然,它无法识别其中一个参数:
"Message": "An error has occurred.",
"ExceptionMessage": "Value cannot be null.\r\nParameter name: entity",
"ExceptionType": "System.ArgumentNullException",
这是处理此Post请求的方法:
[Route("api/locations/postlocation/{id:int}/{lat}/{lon}")]
public IHttpActionResult UpdateUserLocation(string lat, string lon, int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = db.Users.FirstOrDefault(u => u.Id == id);
if (user == null)
{
return NotFound();
}
var userId = user.Id;
var newLocation = new Location
{
Latitude = Convert.ToDouble(lat),
Longitude = Convert.ToDouble(lon),
User = user,
UserId = user.Id,
Time = DateTime.Now
};
var postLocation = PostLocation(newLocation);
return Ok();
}
知道问题出在哪里吗?控制器操作不知道如何查找查询字符串参数。您必须明确地定义它们
[Route("api/locations/postlocation")]
public IHttpActionResult UpdateUserLocation([FromUri] int id, [FromUri] string lat, [FromUri] string lon)
注意,这将中断您的第一个(RESTful)调用示例。如果您同时添加两个
Route
s,则不会中断第一个示例。