diff --git a/SharpSite.Web/Program.cs b/SharpSite.Web/Program.cs index 392bc3f..4014ee2 100644 --- a/SharpSite.Web/Program.cs +++ b/SharpSite.Web/Program.cs @@ -36,6 +36,7 @@ .AddInteractiveServerRenderMode(); app.MapSiteMap(); +app.MapRobotsTxt(); app.MapDefaultEndpoints(); app.Run(); diff --git a/SharpSite.Web/RobotsTxt.cs b/SharpSite.Web/RobotsTxt.cs new file mode 100644 index 0000000..4f0540d --- /dev/null +++ b/SharpSite.Web/RobotsTxt.cs @@ -0,0 +1,33 @@ +using System.Text; + +namespace SharpSite.Web; + +public static class Program_RobotsTxt +{ + + public static WebApplication MapRobotsTxt(this WebApplication app) + { + app.MapGet("/robots.txt", async (HttpContext context) => + { + context.Response.ContentType = "text/plain"; + + var sb = new StringBuilder(); + sb.AppendLine("User-agent: *"); + sb.AppendLine("Disallow: /admin/"); + + // add a line for the sitemap using the base URL from the context + sb.AppendLine($"Sitemap: {context.Request.Scheme}://{context.Request.Host}/sitemap.xml"); + + await context.Response.WriteAsync(sb.ToString()); + }).CacheOutput(policy => + { + policy.Tag("robots"); + policy.Expire(TimeSpan.FromDays(30)); + }); + + + return app; + + } + +} \ No newline at end of file