private static async Task GenerateCollectionResultValue <T>(ResultExecutingContext context, PageList <T> records, string routName) { if (!records.Any() || context.Result is not ObjectResult resultFromAction) { return; } GeneratePaginationHeader(context, records.TotalCount, records.PageSize, records.CurrentPage, records.TotalPages); var linkGen = context.HttpContext.RequestServices.GetRequiredService <LinkGenerator>(); // Get resource collection parameter var paraDesc = context.ActionDescriptor.Parameters.FirstOrDefault(t => t.Name.IsCollectionParameter()); object parameter = null; if (paraDesc != null && context.Controller is Controller controller) { parameter = Activator.CreateInstance(paraDesc.ParameterType); if (parameter != null) { await controller.TryUpdateModelAsync(parameter, paraDesc.ParameterType, ""); } } if (parameter is ResourceCollectionParameters collectionResourcePara) { var links = records.CreatePaginationLinks(routName, context, linkGen, collectionResourcePara); resultFromAction.Value = new { value = records.ShapeData(collectionResourcePara.Fields), links }; } }
public IActionResult GetAuthors(AuthorsResourceParameters authorsResourceParameters) { if (!_propertyMappingService.ValidMappingExistsFor <AuthorDto, Author>(authorsResourceParameters.OrderBy)) { return(BadRequest()); } if (!_typeHelperService.TypeHasProperties <AuthorDto>(authorsResourceParameters.Fields)) { return(BadRequest()); } PageList <Author> authorsFromRepo = _libraryRepository.GetAuthors(authorsResourceParameters); if (authorsFromRepo == null || !authorsFromRepo.Any()) { return(NotFound()); } var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); var authors = Mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo); var links = CreateLinksForAuthors(authorsResourceParameters, authorsFromRepo.HasNext, authorsFromRepo.HasPrevious); var shapedAuthors = authors.ShapeData(authorsResourceParameters.Fields); var shapedAuthorsWithLinks = shapedAuthors.Select(author => { var authorAsDictionary = author as IDictionary <string, object>; var authorLinks = CreateLinksForAuthor( (Guid)authorAsDictionary["Id"], authorsResourceParameters.Fields); authorAsDictionary.Add("links", authorLinks); return(authorAsDictionary); }); var linkedCollectionResource = new { value = shapedAuthorsWithLinks, links = links }; return(Ok(linkedCollectionResource)); }
/// <summary> /// 检测号码的合法性 /// </summary> /// <param name="mobile"></param> /// <returns></returns> public static bool CheckMobile(string mobile) { if (string.IsNullOrEmpty(mobile) || mobile.Length < 10 || mobile.Length > 11) { return(false); } string mobileFirst = mobile.Substring(0, 1); int mobilefirstInt; if (!int.TryParse(mobileFirst, out mobilefirstInt)) { return(false); } if (mobilefirstInt != 0 || mobilefirstInt != 1) { return(false); } PageList <SmsTelesegInfo> telesegs = CacheHelper.Get <PageList <SmsTelesegInfo> >(TelesegCachePre); if (telesegs == null) { telesegs = SmsTelesegManage.Instance.FindList(-1, 0); if (telesegs != null && telesegs.Count > 0) { CacheHelper.Insert(TelesegCachePre, telesegs); } } string mobile3Pre = mobile.Substring(0, 3); if (telesegs != null) { if (telesegs.Any(smsTeleseg => smsTeleseg.Phone == mobile3Pre)) { return(true); } } return(true); }