我從一個不允許我在發送前編輯的設備獲取發布到我的 C# REST API 的資料。我想做的是在控制器上進行編輯。目前正在發送的資料是這樣的:
{
"id": 479,
"imo": "9455052",
"positionReceived": "2022-03-11 19:52 UTC26 minutes ago",
"vesselsLocalTime": "2022-03-11 20:52 LT (UTC 1)",
"area": "WMED - West Mediterranean",
"currentPort": "FOS SUR MER ANCH",
"latitudeLongitude": "43.39003° / 4.959233°",
"status": "At Anchor",
"speedCourse": "0.1 kn / 331 °",
"aisSource": "1706",
"nearByVessels": null,
"weatherWind": "Wind: 31 knots",
"weatherWindDirection": "SE (129°)",
"weatherAirTemperature": "14°C",
"voyageStart": "ATD: 2022-03-06 17:12 LT (UTC 1)",
"voyageStartCountry": "DZ",
"voyageStartPort": "ALG",
"voyageEnd": null,
"voyageEndCountry": "FR",
"voyageEndPort": "FOS SUR MER ANCH",
"voyageGeneralLocation": null,
"voyageStartReportedActualTimeDeparture": "ATD: 2022-03-06 17:12 LT (UTC 1)",
"voyageEndReportedActualTimeArrival": "ATA: 2022-03-08 04:28 LT (UTC 1)"
}
我需要了解的是編輯資料的程序,例如
positionReceived: 2022-03-11 19:52 UTC26 minutes ago",
我想存盤 2022-03-11 19:52 UTC
namespace Statistics.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DatapointsController : ControllerBase
{
private IDatapointRepository datapoints;
public DatapointsController(IDatapointRepository _datapoints)
{
this.datapoints = _datapoints;
}
[HttpGet]
public ActionResult<IEnumerable<Datapoint>> GetAllDatapoints()
{
return datapoints.GetAllDatapoints();
}
[HttpGet("{id}")]
public ActionResult<Datapoint> GetDatapoint(int id)
{
var datapoint = datapoints.GetDatapoint(id);
if (datapoint == null)
{
return NotFound();
}
return datapoint;
}
[EnableCors("CorsPolicy")]
[HttpPost]
public ActionResult<Datapoint> PostDatapoint(Datapoint datapoint)
{
if (datapoints.AddNewDatapoint(datapoint))
{
return datapoint;
}
return BadRequest();
}
}
uj5u.com熱心網友回復:
這個流程有什么問題?
[EnableCors("CorsPolicy")]
[HttpPost]
public ActionResult<Datapoint> PostDatapoint(Datapoint datapoint)
{
datapoint.positionReceived = ... ; // here you can set whatever you want before saving
if (datapoints.AddNewDatapoint(datapoint))
{
return datapoint;
}
return BadRequest();
}
有很多方法可以轉換2022-03-11 19:52 UTC26 minutes ago"為2022-03-11 19:52 UTC. 您需要先分析所有可能的值,然后選擇最適合您的作業。
示例 1
datapoint.positionReceived = datapoint.positionReceived.Substring(0, 20);
示例 2
datapoint.positionReceived = datapoint.positionReceived.Split("UTC")[0] "UTC";
示例 3
datapoint.positionReceived = datapoint.positionReceived.AsSpan().Slice(0,20).ToString();
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441679.html
