From 9da4dc960a7905e54d8529453486fdd40623da7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20P=C4=B1=C3=A7akc=C4=B1?= <98148844+onurpicakci@users.noreply.github.com> Date: Wed, 31 May 2023 13:36:41 +0300 Subject: [PATCH 1/2] Blog: added redirect if blog name is null --- .../Volo/Blogging/Blogs/BlogAppService.cs | 5 ----- .../src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs | 6 ++++++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs index ee379959dc..4f4bd371ad 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs @@ -33,11 +33,6 @@ namespace Volo.Blogging.Blogs var blog = await BlogRepository.FindByShortNameAsync(shortName); - if (blog == null) - { - throw new EntityNotFoundException(typeof(Blog), shortName); - } - return ObjectMapper.Map(blog); } diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs index 2340d16917..be1bf5779b 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs @@ -44,6 +44,12 @@ namespace Volo.Blogging.Pages.Blog.Posts } Blog = await _blogAppService.GetByShortNameAsync(BlogShortName); + + if (Blog == null) + { + return RedirectToPage("/Blogs/Index"); + } + Posts = (await _postAppService.GetListByBlogIdAndTagNameAsync(Blog.Id, TagName)).Items; PopularTags = (await _tagAppService.GetPopularTagsAsync(Blog.Id, new GetPopularTagsInput {ResultCount = 10, MinimumPostCount = 2})); From db285aac8034c99a42a26d2dd3c235b134683f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20P=C4=B1=C3=A7akc=C4=B1?= <98148844+onurpicakci@users.noreply.github.com> Date: Wed, 31 May 2023 14:30:36 +0300 Subject: [PATCH 2/2] refactoring --- .../Volo/Blogging/Blogs/BlogAppService.cs | 5 +++++ .../Pages/Blogs/Posts/Index.cshtml.cs | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs index 4f4bd371ad..a65d92db30 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs @@ -32,6 +32,11 @@ namespace Volo.Blogging.Blogs Check.NotNullOrWhiteSpace(shortName, nameof(shortName)); var blog = await BlogRepository.FindByShortNameAsync(shortName); + + if (blog == null) + { + throw new EntityNotFoundException(typeof(Blog), shortName); + } return ObjectMapper.Map(blog); } diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs index be1bf5779b..b6cc33034a 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blogs/Posts/Index.cshtml.cs @@ -1,7 +1,9 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; +using Volo.Abp.Domain.Entities; using Volo.Blogging.Blogs; using Volo.Blogging.Blogs.Dtos; using Volo.Blogging.Pages.Blogs.Shared.Helpers; @@ -43,11 +45,13 @@ namespace Volo.Blogging.Pages.Blog.Posts return NotFound(); } - Blog = await _blogAppService.GetByShortNameAsync(BlogShortName); - - if (Blog == null) + try { - return RedirectToPage("/Blogs/Index"); + Blog = await _blogAppService.GetByShortNameAsync(BlogShortName); + } + catch (EntityNotFoundException) + { + return RedirectToPage("/Blogs/Index"); } Posts = (await _postAppService.GetListByBlogIdAndTagNameAsync(Blog.Id, TagName)).Items;