コード例 #1
0
        public async Task <IActionResult> RelatedPosts(int id)
        {
            try
            {
                var tag = await _context.Tags.Where(tag => tag.Id == id).FirstOrDefaultAsync();

                if (tag == null)
                {
                    return(NotFound());
                }



                //return related posts
                return(View(new RelatedPostsViewModel
                {
                    Posts = await _context.PostTags
                            .Where(pt => pt.TagId == tag.Id)
                            .Include(pt => pt.Post).ThenInclude(p => p.Article)
                            .Include(pt => pt.Post).ThenInclude(p => p.Share)
                            .Include(pt => pt.Post).ThenInclude(p => p.Markdown)
                            .Select(pt => new PostSummaryViewModel
                    {
                        Id = pt.Post.Id,
                        Title = pt.Post.Title,
                        Summary = pt.Post.Summary,
                        PostUrl = PostsController.GetPostUrl(pt.Post),
                        ThumbnailUrl = pt.Post.ThumbnailUrl,
                        PublishDateTime = pt.Post.PublishDateTime,
                        LatestUpdateDateTime = pt.Post.LatestUpdateDateTime,
                        Type = PostsController.DetectPostType(pt.Post),
                    })
                            .ToListAsync(),
                    Tag = tag
                }));
            }
            catch (Exception e)
            {
                return(Content(e.Message));
            }
        }