В Sitecore я пытаюсь настроить для нашего клиента способ изменения файла robots.txt из дерева контента. Я пытаюсь настроить действие контроллера MVC, которое сопоставлено с маршрутом «robots.txt» и возвращает содержимое файла. Мой контроллер выглядит так:
public class SeoController : BaseController
{
private readonly IContentService _contentService;
private readonly IPageContext _pageContext;
private readonly IRenderingContext _renderingContext;
public SeoController(IContentService contentService, IPageContext pageContext, IRenderingContext renderingContext, ISitecoreContext glassContext)
: base(glassContext)
{
_contentService = contentService;
_pageContext = pageContext;
_renderingContext = renderingContext;
}
public FileContentResult Robots()
{
string content = string.Empty;
var contentResponse = _contentService.GetRobotsTxtContent();
if (contentResponse.Success && contentResponse.ContentItem != null)
{
content = contentResponse.ContentItem.RobotsText;
}
return File(Encoding.UTF8.GetBytes(content), "text/plain");
}
}
И конфигурация маршрута:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute("Robots.txt", "robots.txt", new { controller = "Seo", action = "Robots" });
}
}
Все это прекрасно работает, если я использую маршрут без расширения «.txt». Однако после добавления расширения я получаю исключение нулевой ссылки на уровне домена из-за того, что база данных контекста имеет значение null. Вот где происходит ошибка:
public Item GetItem(string contentGuid)
{
return Sitecore.Context.Database.GetItem(contentGuid);
}
Я предполагаю, что в sitecore есть настройка, которая игнорирует расширение .txt. Я попытался добавить его как разрешенное расширение в параметре Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions конфигурации. Есть ли что-то еще, что я мог упустить?