1、在header里加入分页导航信息
查看此处
2、给返回的Dto加入link,实现HATEOAS
分两步:①给每一个Value都给一个link,根据情况添加post、get、put等信息
②给总的Value添加一个link,包含对总Values的实际操作。
准备工作:创建linkDto对象,用于存储链接
public class LinkDto
{
public string Href { get; set; }
public string Rel { get; set; }
public string Method { get; set; }
public LinkDto(string href, string rel, string method)
{
Href = href;
Rel = rel;
Method = method;
}
}
① 创建子value的link方法
private IEnumerable<LinkDto> CreateLinkForTouristRoute(
Guid touristRouteId,
string fields)
{
List<LinkDto> links = new List<LinkDto>();
links.Add(
new LinkDto(
Url.Link("GetTouristRouteById", new { touristRouteId, fields }),
"self",
"GET"
)
);
// 更新
links.Add(
new LinkDto(
Url.Link("UpdateTouristRoute", new { touristRouteId }),
"update",
"PUT"
)
);
// 局部更新
links.Add(
new LinkDto(
Url.Link("PartiallyUpdateTouristRoute", new { touristRouteId }),
"partially_update",
"PATCH")
);
// 删除
links.Add(
new LinkDto(
Url.Link("DeleteTouristRoute", new { touristRouteId }),
"delete",
"DELETE")
);
// 获取路线图片
links.Add(
new LinkDto(
Url.Link("GetPictureListForTouristRoute", new { touristRouteId }),
"get_pictures",
"GET")
);
// 添加新图片
links.Add(
new LinkDto(
Url.Link("CreateTouristRoutePicture", new { touristRouteId }),
"create_picture",
"POST")
);
return links;
}
②创建总的Values的link 方法
private IEnumerable<LinkDto> CreateLinksForTouristRouteList(TouristRouteResourceParamaters paramaters, PaginationResourceParamaters paramaters2)
{
List<LinkDto> links = new List<LinkDto>
{
// 添加self,自我链接
new LinkDto(_urlHelper.GenerateTouristRouteResourceURL(paramaters, paramaters2, ResourceUriType.CurrnetPage),"self","GET"),//此处的方法为给header创建连接的方法
// "api/touristRoutes"
// 添加创建旅游路线
new LinkDto(Url.Link("CreateTouristRoute", null),"create_tourist_route","POST")
};
return links;
}
③在使用中调用方法:
IEnumerable<LinkDto> linkDto = CreateLinksForTouristRouteList(paramaters, paramaters2);
IEnumerable<IDictionary<string, object>> shapedDtoWithLinklist = shapedDtoList.Select(t =>
{
IDictionary<string, object> touristRouteDictionary = t;
IEnumerable<LinkDto> links = CreateLinkForTouristRoute(
(Guid)touristRouteDictionary["Id"], null);
touristRouteDictionary.Add("links", links);
return touristRouteDictionary;
});
var result = new
{
value = shapedDtoWithLinklist,
links = linkDto
};
return Ok(result);