diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2023-06-14 07:45:14 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2023-06-14 07:45:14 +0300 |
commit | a5bf29c00b85358f5cc078422871aa6654866d0c (patch) | |
tree | 177c69399bb1b1ae680265f5c0dedbdbf8305587 | |
parent | afdaabce8fb24f6fd46e387445c076f67a0c98ed (diff) |
Add robots.txt generator
-rw-r--r-- | Program.cs | 7 | ||||
-rw-r--r-- | Services/IRobotsTxtGenerator.cs | 21 |
2 files changed, 27 insertions, 1 deletions
@@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using MyDarling.Models; using MyDarling.Services; using Microsoft.AspNetCore.Identity; - +using MyDarling.Controllers; var builder = WebApplication.CreateBuilder(args); @@ -28,6 +28,7 @@ builder.Services.Configure<IdentityOptions>( opts => }); builder.Services.AddTransient<IImageResizer, ImageResizer>(); +builder.Services.AddScoped<IRobotsTxtGenerator, RobotsTxtGenerator>(); builder.Services.AddControllersWithViews(); builder.Services.AddRazorPages(); @@ -38,6 +39,10 @@ app.MapControllers(); app.MapDefaultControllerRoute(); app.MapRazorPages(); +var robotsScope = app.Services.CreateScope(); +var robotsGenerator = robotsScope.ServiceProvider.GetService<IRobotsTxtGenerator>(); +app.MapGet("/robots.txt", () => robotsGenerator!.GetRobotsText()); + app.UseAuthentication(); app.UseAuthorization(); IdentitySeedData.CreateAdminAccount(app.Services, app.Configuration); diff --git a/Services/IRobotsTxtGenerator.cs b/Services/IRobotsTxtGenerator.cs new file mode 100644 index 0000000..a238a8e --- /dev/null +++ b/Services/IRobotsTxtGenerator.cs @@ -0,0 +1,21 @@ +using System.Text; + +namespace MyDarling.Controllers; + +public interface IRobotsTxtGenerator +{ + public string GetRobotsText(); +} + +public class RobotsTxtGenerator : IRobotsTxtGenerator +{ + public string GetRobotsText() + { + StringBuilder stringBuilder = new(); + stringBuilder.AppendLine("user-agent: *"); + stringBuilder.AppendLine("disallow: /freedom"); + stringBuilder.AppendLine("disallow: /Account/"); + + return stringBuilder.ToString(); + } +} |