public async Task <IActionResult> PagedUsers([FromQuery] UserPagedCollectionQueryRequest request, [FromHeader(Name = "Accept")] string accept) { var includeLinks = LinkerFactory.IncludeLinks(accept); if (int.TryParse(_config["MaxPageSize"], out var maxPageSize)) { request.MaxPageSize = maxPageSize; } //查询业务员,进行查询得到结果 var queryResponse = await _mediator.Send(request); //查询出错 if (queryResponse.Status != HttpStatusCode.OK) { return(Problem(queryResponse.Message, nameof(PagedUsers), (int)queryResponse.Status, "Request occur error")); } //写Header Response.Headers.Add("X-Pagination", queryResponse.MoreInfo); //Hateaos:创建自驱动链接 if (includeLinks) { var linkerFactory = new LinkerFactory(Url); var links = linkerFactory.CreateLinksForCollections(request, nameof(PagedUsers), request.HasPreviousPage, request.HasNextPage); var shapedDatasWithLinks = queryResponse.ShapedData.Select(items => { var dict = items as IDictionary <string, object>; var itemLinks = linkerFactory.CreateLinksForItem((long)dict["Id"], null, GetCurrentControllerItemLinker()); dict.Add("links", itemLinks); return(dict); }); var createLink = linkerFactory.CreatePostItemLink(nameof(CreateUser)); var linkedCollectionResource = new { value = shapedDatasWithLinks, page_navigator = links, create_new_entity = createLink }; return(Ok(linkedCollectionResource)); } return(Ok(queryResponse.ShapedData)); }
public async Task <IActionResult> GetUsers([FromQuery] UserCollectionQueryRequest request, [FromHeader(Name = "Accept")] string mediaType) { var includeLinks = LinkerFactory.IncludeLinks(mediaType); //查询业务员 var queryResponse = await _mediator.Send(request); if (queryResponse.Status != HttpStatusCode.OK) { return(Problem(queryResponse.Message, nameof(GetUsers), (int)queryResponse.Status, "Request occur error")); } //Hateaos:创建自驱动链接 IEnumerable <IDictionary <string, object> > shapedItemsWithLinks = null; if (includeLinks) //如果返回参数需要包含links { var linkerFactory = new LinkerFactory(Url); shapedItemsWithLinks = queryResponse.ShapedData.Select(item => { var dict = item as IDictionary <string, object>; var itemLinks = linkerFactory.CreateLinksForItem((long)dict["Id"], null, GetCurrentControllerItemLinker()); dict.Add("links", itemLinks); return(dict); }); var createLink = linkerFactory.CreatePostItemLink(nameof(CreateUser)); var linkedCollectionResource = new { value = shapedItemsWithLinks, create_new_entity = createLink }; return(Ok(linkedCollectionResource)); } shapedItemsWithLinks = queryResponse.ShapedData.Select(opr => { var oprDict = opr as IDictionary <string, object>; return(oprDict); }); return(Ok(shapedItemsWithLinks)); }