BCards/src/BCards.Web/Controllers/SitemapController.cs
Ricardo Carneiro 3d2ce1f8cf
All checks were successful
BCards Deployment Pipeline / Run Tests (push) Successful in 4s
BCards Deployment Pipeline / PR Validation (push) Has been skipped
BCards Deployment Pipeline / Build and Push Image (push) Successful in 15m22s
BCards Deployment Pipeline / Deploy to Production (ARM - OCI) (push) Successful in 1m54s
BCards Deployment Pipeline / Deploy to Test (x86 - Local) (push) Has been skipped
BCards Deployment Pipeline / Cleanup Old Resources (push) Has been skipped
BCards Deployment Pipeline / Deployment Summary (push) Successful in 0s
fix: Increase session timeout to 7 days and set SameSite=None for Cloudflare compatibility
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-18 12:32:42 -03:00

96 lines
3.6 KiB
C#

using Microsoft.AspNetCore.Mvc;
using System.Text;
using System.Xml.Linq;
using BCards.Web.Services;
namespace BCards.Web.Controllers;
public class SitemapController : Controller
{
private readonly IUserPageService _userPageService;
private readonly ILivePageService _livePageService;
private readonly ILogger<SitemapController> _logger;
public SitemapController(
IUserPageService userPageService,
ILivePageService livePageService,
ILogger<SitemapController> logger)
{
_userPageService = userPageService;
_livePageService = livePageService;
_logger = logger;
}
[Route("sitemap.xml")]
[ResponseCache(Duration = 86400)] // Cache for 24 hours
public async Task<IActionResult> Index()
{
try
{
// 🔥 NOVA FUNCIONALIDADE: Usar LivePages em vez de UserPages
var livePages = await _livePageService.GetAllActiveAsync();
// Define namespace corretamente para evitar conflitos
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
// Construir URLs das páginas dinâmicas separadamente para evitar problemas
var dynamicUrls = livePages.Select(page =>
new XElement(ns + "url",
new XElement(ns + "loc", $"{Request.Scheme}://{Request.Host}/page/{page.Category?.Replace(" ", "-")?.ToLower()}/{page.Slug}"),
new XElement(ns + "lastmod", page.LastSyncAt.ToString("yyyy-MM-dd")),
new XElement(ns + "changefreq", "weekly"),
new XElement(ns + "priority", "0.8")
)
).ToList();
var sitemap = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement(ns + "urlset",
// Add static pages
new XElement(ns + "url",
new XElement(ns + "loc", $"{Request.Scheme}://{Request.Host}/"),
new XElement(ns + "lastmod", DateTime.UtcNow.ToString("yyyy-MM-dd")),
new XElement(ns + "changefreq", "daily"),
new XElement(ns + "priority", "1.0")
),
new XElement(ns + "url",
new XElement(ns + "loc", $"{Request.Scheme}://{Request.Host}/Home/Pricing"),
new XElement(ns + "lastmod", DateTime.UtcNow.ToString("yyyy-MM-dd")),
new XElement(ns + "changefreq", "weekly"),
new XElement(ns + "priority", "0.9")
),
// Add live pages (SEO-optimized URLs only)
dynamicUrls
)
);
_logger.LogInformation($"Generated sitemap with {livePages.Count} live pages");
return Content(sitemap.ToString(SaveOptions.DisableFormatting), "application/xml", Encoding.UTF8);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error generating sitemap");
return StatusCode(500, "Error generating sitemap");
}
}
[Route("robots.txt")]
[ResponseCache(Duration = 86400)] // Cache for 24 hours
public IActionResult RobotsTxt()
{
var robotsTxt = $@"User-agent: *
Allow: /
Allow: /page/
Disallow: /Admin/
Disallow: /Auth/
Disallow: /Payment/
Disallow: /api/
Sitemap: {Request.Scheme}://{Request.Host}/sitemap.xml";
return Content(robotsTxt, "text/plain", Encoding.UTF8);
}
}