From 6a46a763f6bbf791b179a8744814e7a44996ba6a Mon Sep 17 00:00:00 2001 From: Chase Burandt Date: Thu, 9 Mar 2023 17:01:08 -0600 Subject: [PATCH] Fixed a couple of bugs in the robots.txt file. --- Blend.RobotsTxt/RobotsTxtBuilder.cs | 13 ++++++------- Blend.RobotsTxt/RobotsTxtOption.cs | 7 ++----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Blend.RobotsTxt/RobotsTxtBuilder.cs b/Blend.RobotsTxt/RobotsTxtBuilder.cs index 0389959..1ef9d45 100644 --- a/Blend.RobotsTxt/RobotsTxtBuilder.cs +++ b/Blend.RobotsTxt/RobotsTxtBuilder.cs @@ -16,13 +16,12 @@ public interface IRobotsBuilder public class RobotsTxtBuilder : IRobotsBuilder { private readonly List _config; - private readonly GlobalSettings _globalSettings; private readonly IWebHostEnvironment _webHostEnvironment; - public RobotsTxtBuilder(IOptions> config, IOptions globalSettings, IWebHostEnvironment webHostEnvironment) + public RobotsTxtBuilder(IOptions> config, IWebHostEnvironment webHostEnvironment) { _config = config.Value; - _globalSettings = globalSettings.Value; + _webHostEnvironment = webHostEnvironment; } @@ -53,7 +52,8 @@ public string BuildRobots() { foreach (var item in robot.Allow) { - stringBuilder.AppendLine($"Allow: {item}"); + if (!item.IsNullOrWhiteSpace()) + stringBuilder.AppendLine($"Allow: {item}"); } } @@ -62,7 +62,8 @@ public string BuildRobots() { foreach (var item in robot.Disallow) { - stringBuilder.AppendLine($"Disallow: {item}"); + if (!item.IsNullOrWhiteSpace()) + stringBuilder.AppendLine($"Disallow: {item}"); } } @@ -82,7 +83,6 @@ public string BuildRobots() case "Production": stringBuilder.AppendLine("User-agent: *"); stringBuilder.AppendLine("Allow: /"); - stringBuilder.AppendLine($"Disallow: {_globalSettings.UmbracoPath.Replace("~", "")}"); break; default: stringBuilder.AppendLine("User-agent: *"); @@ -96,7 +96,6 @@ public string BuildRobots() // Last Resort and display production robots.txt. stringBuilder.AppendLine("User-agent: *"); stringBuilder.AppendLine("Allow: /"); - stringBuilder.AppendLine($"Disallow: {_globalSettings.UmbracoPath.Replace("~", "")}"); } var robotsTxt = stringBuilder.ToString(); return robotsTxt; diff --git a/Blend.RobotsTxt/RobotsTxtOption.cs b/Blend.RobotsTxt/RobotsTxtOption.cs index ffc3909..a77791e 100644 --- a/Blend.RobotsTxt/RobotsTxtOption.cs +++ b/Blend.RobotsTxt/RobotsTxtOption.cs @@ -20,21 +20,18 @@ public class RobotsTxtOption /// /// Array of paths to allow /// - [DefaultValue(new string[] { })] [Description("Array of paths to allow")] - public string[] Allow { get; set; } + public string[] Allow { get; set; } = Array.Empty(); /// /// Array of paths to disallow /// - [DefaultValue(new string[] { "/" })] [Description("Array of paths to allow")] - public string[] Disallow { get; set; } + public string[] Disallow { get; set; } = Array.Empty(); /// /// Path to the sitemap.xml file /// - [DefaultValue("")] [Description("Path to the sitemap.xml file")] public string Sitemap { get; set; }