Skip to content

Commit

Permalink
added initial robots.txt (#41)
Browse files Browse the repository at this point in the history
* added sitemap to robots.txt

* removed sitemap

* moved robots.txt to a minimal API
  • Loading branch information
csharpfritz authored Oct 29, 2024
1 parent 3d77285 commit 79921ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions SharpSite.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
.AddInteractiveServerRenderMode();

app.MapSiteMap();
app.MapRobotsTxt();
app.MapDefaultEndpoints();

app.Run();
33 changes: 33 additions & 0 deletions SharpSite.Web/RobotsTxt.cs
Original file line number Diff line number Diff line change
@@ -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;

}

}

0 comments on commit 79921ba

Please sign in to comment.