-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added sitemap to robots.txt * removed sitemap * moved robots.txt to a minimal API
- Loading branch information
1 parent
3d77285
commit 79921ba
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
.AddInteractiveServerRenderMode(); | ||
|
||
app.MapSiteMap(); | ||
app.MapRobotsTxt(); | ||
app.MapDefaultEndpoints(); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
} |