HttpCilent跨服务请求

HttpCilent跨服务请求

需要先引用 S.Common 调用里面的 HttpClient 帮助类

注意:不能直接引用别人的Api,如需传参数自己定义Dto,或者拼接参数

Get

/// <summary>

/// 跨服务请求角色列表

/// </summary>

/// <returns></returns>

[HttpGet("GetPageRole")]

public IActionResult GetPageRole(int page=1,int size=5,string? name="")

{

var list = HttpClientHelper.GetResponse($"https://localhost:7109/api/RoleMenuRead/GetPageRole?pageIndex={page}&pageSize={size}&roleName={name}");

return Ok(list);

}

descript

如果需要使用获取到的值显示,需要定义Dto 进行反序列化

descript

/// <summary>

/// 跨服务请求角色列表

/// </summary>

/// <returns></returns>

[HttpGet("GetPageRole")]

public IActionResult GetPageRole(int page = 1, int size = 5, string? name = "")

{

var jsonString = HttpClientHelper.GetResponse($"https://localhost:7109/api/RoleMenuRead/GetPageRole?pageIndex={page}&pageSize={size}&roleName={name}");

// 反序列化并显示分页数据

var response = JsonConvert.DeserializeObject<ResponseDto>(jsonString);

return Ok(response);

}

public class ResponseDto

{

public List<RoleAddDto> RoleList { get; set; }

public int PageCount { get; set; }

public int TotalCount { get; set; }

}

Post

/// <summary>

/// 跨服务添加角色

/// </summary>

/// <param name="role"></param>

/// <returns></returns>

///

[HttpPost("AddRole")]

public IActionResult AddRole(RoleAddDto role)

{

var result = HttpClientHelper.PostResponse("https://localhost:7026/api/RoleMenu/AddRole", role);

return Ok(result);

}

descript

Delete

/// <summary>

/// 跨服务删除耳标

/// </summary>

/// <returns></returns>

///

[HttpPost("DeleteRole")]

public IActionResult DeleteEarTag(int id)

{

var result = HttpClientHelper.DeleteResponse($"http://localhost:5283/api/EarTag/DeleteEarTag?id={id}");

return Ok(result);

}

descript

Put

/// <summary>

/// 跨服务修改角色

/// </summary>

/// <param name="role"></param>

/// <returns></returns>

[HttpPut("UpdateRole")]

public IActionResult UpdateRole(RoleAddDto role)

{

var reqult = HttpClientHelper.PutDataResponse("https://localhost:7026/api/RoleMenu/UpdateRole", role);

return Ok(reqult);

}

descript

posted @ 2024-06-05 09:58  北落师门、  阅读(14)  评论(0编辑  收藏  举报