-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom UmbracoPageController using custom routes can no longer get UmbracoContext #16969
Comments
Hi there @jamesrichardbrett! Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better. We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.
We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions. Thanks, from your friendly Umbraco GitHub bot 🤖 🙂 |
We are experiencing the same issue on 13.5.2 but only with Umbraco instances that contain multiple sites |
Reproduced with slighly simplified code public class RobotsTxtController : UmbracoPageController
{
public static string[] RoutePatterns = new[] { "/robots.txt", "/{local}/robots.txt" };
public RobotsTxtController(ILogger<RobotsTxtController> logger, ICompositeViewEngine compositeViewEngine)
: base(logger, compositeViewEngine)
{
}
public IActionResult Index()
{
var robotsDefaultContent = "User-agent: *\n Disallow: /";
if (CurrentPage is SiteRoot { AllowCrawlers: true } root)
{
var overrideRobotsTxtContent = root.OverrideRobotstxtContent;
if (!string.IsNullOrWhiteSpace(overrideRobotsTxtContent))
{
robotsDefaultContent = overrideRobotsTxtContent;
}
else
{
robotsDefaultContent = "User-agent: *\nDisallow: /app_plugins/\nDisallow: /umbraco/";
}
var sitemap = CurrentPage?.FirstChild<Sitemap>();
if (sitemap != null)
{
robotsDefaultContent += $"\n\nSitemap: {sitemap.Url(mode: UrlMode.Absolute)}";
}
}
return Content(robotsDefaultContent, "text/plain", Encoding.UTF8);
}
}
public class RobotsComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.Configure<UmbracoPipelineOptions>(options =>
{
options.AddFilter(new UmbracoPipelineFilter(nameof(RobotsTxtController))
{
Endpoints = app => app.UseEndpoints(endpoints =>
{
for (int i = 0; i < RobotsTxtController.RoutePatterns.Length; i++)
{
endpoints.MapControllerRoute(
$"{nameof(RobotsTxtController)}_{i}",
RobotsTxtController.RoutePatterns[i],
new
{
Controller = ControllerExtensions.GetControllerName<RobotsTxtController>(),
Action = nameof(RobotsTxtController.Index)
})
.ForUmbracoPage(FindContent);
}
})
});
});
}
private IPublishedContent FindContent(ActionExecutingContext actionExecutingContext)
{
// Resolve services from the container
var umbracoContextAccessor = actionExecutingContext.HttpContext.RequestServices
.GetRequiredService<IUmbracoContextAccessor>();
using IUmbracoContext umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
var domain = DomainUtilities.SelectDomain(umbracoContext.Domains?.GetAll(false),
umbracoContext.CleanedUmbracoUrl);
if (domain == null)
return DefaultToFirstRoot(umbracoContext);
var content = umbracoContext.Content?.GetById(domain.ContentId);
return content ?? DefaultToFirstRoot(umbracoContext);
}
private IPublishedContent DefaultToFirstRoot(IUmbracoContext umbracoContext)
{
return umbracoContext.Content?.GetAtRoot().FirstOrDefault() ??
throw new InvalidOperationException("Umbraco has no content nodes to find");
}
} And also using the public class RobotsTxtController :UmbracoPageController, IVirtualPageController
{
public RobotsTxtController(ILogger<UmbracoPageController> logger, ICompositeViewEngine compositeViewEngine) : base(logger, compositeViewEngine)
{
}
[HttpGet]
[Route("/robots.txt")]
[Route("/{local}/robots.txt")]
public IActionResult Index()
{
var robotsDefaultContent = "User-agent: *\n Disallow: /";
if (CurrentPage is SiteRoot { AllowCrawlers: true } root)
{
var overrideRobotsTxtContent = root.OverrideRobotstxtContent;
if (!string.IsNullOrWhiteSpace(overrideRobotsTxtContent))
{
robotsDefaultContent = overrideRobotsTxtContent;
}
else
{
robotsDefaultContent = "User-agent: *\nDisallow: /app_plugins/\nDisallow: /umbraco/";
}
var sitemap = CurrentPage?.FirstChild<Sitemap>();
if (sitemap != null)
{
robotsDefaultContent += $"\n\nSitemap: {sitemap.Url(mode: UrlMode.Absolute)}";
}
}
return Content(robotsDefaultContent, "text/plain", Encoding.UTF8);
}
public IPublishedContent? FindContent(ActionExecutingContext actionExecutingContext)
{
// Resolve services from the container
var umbracoContextAccessor = actionExecutingContext.HttpContext.RequestServices
.GetRequiredService<IUmbracoContextAccessor>();
using IUmbracoContext umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
var domain = DomainUtilities.SelectDomain(umbracoContext.Domains?.GetAll(false),
umbracoContext.CleanedUmbracoUrl);
if (domain == null)
return DefaultToFirstRoot(umbracoContext);
var content = umbracoContext.Content?.GetById(domain.ContentId);
return content ?? DefaultToFirstRoot(umbracoContext);
}
private IPublishedContent DefaultToFirstRoot(IUmbracoContext umbracoContext)
{
return umbracoContext.Content?.GetAtRoot().FirstOrDefault() ??
throw new InvalidOperationException("Umbraco has no content nodes to find");
}
} |
Hey @jamesrichardbrett, we have optimized when how/when parts of the umbraco pipeline runs. One of such optimizations is to not do it when a request is considered a front end request (images, static files,...) by defining the rout as .txt you are implying to asp.net core that it is a client side file. You can overwrite our ignoring client side files for specific cases by adding this to your composer as documented over here https://docs.umbraco.com/umbraco-cms/reference/routing/custom-routes#client-side-requests. builder.Services.Configure<UmbracoRequestOptions>(options =>
{
options.HandleAsServerSideRequest = httpRequest => httpRequest.Path.Value?.EndsWith("/robots.txt") == true;
}); Feel free to reopen the ticket if this would not solve your issue. |
Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)
13.4.0
Bug summary
After upgrading to Umbraco 13.4.0 can no longer get the Umbraco Context in a Custom UmbracoPageController
An unhandled exception occurred while processing the request.
InvalidOperationException: Wasn't able to get an UmbracoContext
Umbraco.Extensions.UmbracoContextAccessorExtensions.GetRequiredUmbracoContext(IUmbracoContextAccessor umbracoContextAccessor)
Downgrading back to 13.3.1 fixes the issue
here is the code
Composer
Specifics
This currently work fine in live which is still 13.3.1
DEV and UAT environments on 13.4.0 both error
Steps to reproduce
Visit the page /robots.txt
Expected result / actual result
No response
The text was updated successfully, but these errors were encountered: