Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matthew Wise 271 posts 1373 karma points MVP 5x c-trib
    Feb 17, 2020 @ 16:11
    Matthew Wise
    0

    Using UmbracoVirtualNodeRouteHandler for routing robots.txt

    Hi

    I am trying to add a custom route for robots.txt following - https://our.umbraco.com/documentation/reference/routing/custom-routes#custom-routes-within-the-umbraco-pipeline

    However as its a file path the UmbracoContext is null in v7 you could create your own context to solve this issue, this has been removed in v8.

    How can I achieve this?

    v7 code

    protected override UmbracoContext GetUmbracoContext(RequestContext requestContext)
        {
            var ctx = base.GetUmbracoContext(requestContext);
            if (ctx != null) return ctx;
    
            var httpContext = requestContext.HttpContext;
    
            ctx = UmbracoContext.EnsureContext(httpContext,
                ApplicationContext,
                new WebSecurity(httpContext, ApplicationContext),
                UmbracoConfig.For.UmbracoSettings(),
                UrlProviderResolver.Current.Providers,
                false,
                false);
    
            return ctx;
        }
    
  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Feb 17, 2020 @ 16:21
    Kevin Jump
    1

    Hi ,

    I believe you use the IUmbracoContextFactory to ensure the context.

    So Ideally through Dependency Injection and then

    var contextRefrence = umbracoContextFactory.EnsureUmbracoContext()
    

    and at a push Umbraco.Web.Composing.Current.UmbracoContext

    There are more details in this PR comment : https://github.com/umbraco/Umbraco-CMS/issues/4572#issuecomment-463595962

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 17, 2020 @ 16:21
    Matt Brailsford
    2

    You'll want to resolve an IUmbracoContextFactory and call it's EnsureUmbracoContext() method

    using (_umbracoContextFactory.EnsureUmbracoContext())
    {
        // Do your thing
    }
    
  • Matthew Wise 271 posts 1373 karma points MVP 5x c-trib
    Feb 17, 2020 @ 17:04
    Matthew Wise
    0

    Thanks both! (who gets the marked answer???)

    For anyone who is trying to achieve the same result Callum has a package which you can use or view the source of :)

    https://github.com/callumbwhyte/friendly-robots

Please Sign in or register to post replies

Write your reply to:

Draft