public ActionResult OnGet(string category) { if (!_context.Categories.Any(c => c.Name == category)) { return(NotFound()); } Category = _context.Categories.Single(c => c.Name == category); int categoryId = Category.Id; int languageId = _context.GetLanguageId(); const int indexCategory = 1; //TODO: Get (numbered) list of blog posts with titles. Posts = _context.Posts .Where(p => p.CategoryId == categoryId) .Include(p => p.Content.Where(c => c.LanguageId == languageId)) .Where(p => p.Content.Count != 0 && p.Public == true) .ToList(); //Get index post, don't get if on next page? Posts.Insert(0, _context.Posts .Where(p => p.CategoryId == indexCategory && p.Title == Category.Name) .Include(p => p.Content.Where(c => c.LanguageId == languageId)) .FirstOrDefault()); if (Posts[0] == null) { return(NotFound()); } return(Page()); }
public ActionResult OnGet(string category, int id) { if (!_context.Posts.Any(p => p.Id == id) && !_context.Categories.Any(c => c.Name == category)) { return(NotFound()); } Category = _context.Categories.Single(c => c.Name == category); //Both checking for any and placing category in object, is there a better way? Put category in object, then check for null? Do 2 context queries even slow things down? int languageId = _context.GetLanguageId(); Post = _context.Posts .Where(p => p.Id == id) .Include(p => p.Content.Where(c => c.LanguageId == languageId)) .FirstOrDefault(); //TODO: Create check if post actually has content to display if (Post.Content.Count == 0) { return(NotFound()); } return(Page()); }