1、創建webapi項目,提供介面方法如下:/// /// 獲取租戶、位置下的所有感測器 /// /// [AcceptVerbs("POST")] [Route("api/Sensors/GetSensors")] ...
1、創建webapi項目,提供介面方法如下:
/// <summary>
/// 獲取租戶、位置下的所有感測器
/// </summary>
/// <returns></returns>
[AcceptVerbs("POST")]
[Route("api/Sensors/GetSensors")]
public JsonResponseBase<List<TenSensorDTO>> GetSensors()
{
Logger.Debug("SensorsController---------------GetSensors");
string jsonData = this.Request.Content.ReadAsStringAsync().Result;
Logger.Information(jsonData);
TenSensorSearchDTO search = null;
var response = new JsonResponseBase<List<TenSensorDTO>>();
try
{
search = JsonConvert.DeserializeObject<TenSensorSearchDTO>(jsonData);
response.Json.IsSuccess = true;
}
catch(Exception ex)
{
Logger.Fatal(ex);
response.Json.IsSuccess = false;
response.Json.OperationDesc = "參數錯誤";
}
if (response.Json.IsSuccess && search != null)
{
response = TenantManageService.GetSensorList(search);
}
return response;
}
2、客戶端使用httpclient調用
string url = urlPre + "Sensors/GetSensors";
//租戶ID必須傳入,LocationID可以不傳入,看調用段需求
var content = new { TenantID = Guid.Parse("5ebf5f81-ac69-418c-b170-9eb255201dd1") ,
LocationID = Guid.Parse("db5a58c0-1d12-4fe9-99bb-4bebe6bcb935") };
var requestJson = JsonConvert.SerializeObject(content);
HttpContent httpContent = new StringContent(requestJson);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var httpClient = new HttpClient();
var responseJson = httpClient.PostAsync(url, httpContent)
.Result.Content.ReadAsStringAsync().Result;