0
0

Add robots.txt generator

This commit is contained in:
Sergey Chebotar 2023-06-14 07:45:14 +03:00
parent afdaabce8f
commit a5bf29c00b
2 changed files with 27 additions and 1 deletions

View File

@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore;
using MyDarling.Models; using MyDarling.Models;
using MyDarling.Services; using MyDarling.Services;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using MyDarling.Controllers;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -28,6 +28,7 @@ builder.Services.Configure<IdentityOptions>( opts =>
}); });
builder.Services.AddTransient<IImageResizer, ImageResizer>(); builder.Services.AddTransient<IImageResizer, ImageResizer>();
builder.Services.AddScoped<IRobotsTxtGenerator, RobotsTxtGenerator>();
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
@ -38,6 +39,10 @@ app.MapControllers();
app.MapDefaultControllerRoute(); app.MapDefaultControllerRoute();
app.MapRazorPages(); app.MapRazorPages();
var robotsScope = app.Services.CreateScope();
var robotsGenerator = robotsScope.ServiceProvider.GetService<IRobotsTxtGenerator>();
app.MapGet("/robots.txt", () => robotsGenerator!.GetRobotsText());
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
IdentitySeedData.CreateAdminAccount(app.Services, app.Configuration); IdentitySeedData.CreateAdminAccount(app.Services, app.Configuration);

View File

@ -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();
}
}