Skip to content

[17.0.0-rc1]: IPublishedContent .Url() method throws 'Wasn't able to get an UmbracoContext' #20774

@shearer3000

Description

@shearer3000

Which Umbraco version are you using?

17.0.0-rc1

Bug summary

Using a minimal api style endpoint for producing search engine sitemap content, via the route "/sitemap.xml" for example, will throw an exception of  'Wasn't able to get an UmbracoContext' when trying to resolve the IPublishedContent Url()

Specifics

No response

Steps to reproduce

I've cut down this sample code for brevity and just to highlight/reproduce the specific problem

public static class TestApis
{
  public static IEndpointRouteBuilder MapTestApi(this IEndpointRouteBuilder app)
  {

    app.MapGet("sitemap.xml", TestApis.Get);

    return app;
  }

  private static Ok<List<string>> Get(
    [FromServices] IPublishedContentQuery contentQuery
  )
  {
    var homepageIpcs = contentQuery.ContentAtRoot()
                        .Where(x => x.ContentType.Alias.Contains("homepage", StringComparison.InvariantCultureIgnoreCase));
    var pageIpcs = new List<IPublishedContent>();
    pageIpcs.AddRange(homepageIpcs.SelectMany(s => s.DescendantsOrSelf()));
    var urls = pageIpcs.Select(x=>x.Url());
    return TypedResults.Ok(urls.ToList());
  }
}

When the route is "/sitemap.xml" the exception is produced at runtime. When the route is "/sitemap" (ie. without extension) the route executes successfully i.e. Url() works.

Expected result / actual result

Umbraco context in custom routes is available in the same way whether it is extensionless or not

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions