diff --git a/Program.cs b/Program.cs index d3db670..60f5c15 100644 --- a/Program.cs +++ b/Program.cs @@ -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( opts => }); builder.Services.AddTransient(); +builder.Services.AddScoped(); 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(); +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(); + } +}