I am working on an API that allows users to request html of specific content so that it can be displayed in other applications within our company. The html can include images and media links. Is there a way to force all URLs to be rendered in absolute mode for those specific requests?
Currently, the only solution I can come up with is to change all of my partials so that they accept a parameter that will override the default URL mode every where I use the Url() extension. I would prefer to avoid this if possible.
I came up with a solution for anyone who finds this in the future. My solution involves creating 2 custom url providers that inherit the default providers; a provider inherited from DefaultUrlProvider and DefaultMediaUrlProvider. You need to register these providers with Umbraco builder in startup.
For my implementation, I look for a query string in the current Uri and then change the UrlMode to Absolute if the value is true. This results in every link and image to render with an absolute URL.
public class CustomMediaUrlProvider : DefaultMediaUrlProvider
{
public CustomMediaUrlProvider(MediaUrlGeneratorCollection mediaPathGenerators, UriUtility uriUtility)
: base(mediaPathGenerators, uriUtility)
{
}
public override UrlInfo GetMediaUrl(IPublishedContent content, string propertyAlias, UrlMode mode, string culture, Uri current)
{
var query = HttpUtility.ParseQueryString(current.Query);
if (bool.TryParse(query["absoluteUrls"], out bool results) && results == true)
{
mode = UrlMode.Absolute;
}
return base.GetMediaUrl(content, propertyAlias, mode, culture, current);
}
}
public class CustomUrlProvider : DefaultUrlProvider
{
private readonly IHttpContextAccessor _httpContextAccessor;
public CustomUrlProvider(IOptionsMonitor<RequestHandlerSettings> requestSettings, ILogger<DefaultUrlProvider> logger, ISiteDomainMapper siteDomainMapper, IUmbracoContextAccessor umbracoContextAccessor, UriUtility uriUtility, ILocalizationService localizationService, IHttpContextAccessor httpContextAccessor)
: base(requestSettings, logger, siteDomainMapper, umbracoContextAccessor, uriUtility, localizationService)
{
_httpContextAccessor = httpContextAccessor;
}
public override UrlInfo GetUrl(IPublishedContent content, UrlMode mode, string culture, Uri current)
{
var query = HttpUtility.ParseQueryString(current.Query);
if (bool.TryParse(query["absoluteUrls"], out bool results) && results == true)
{
mode = UrlMode.Absolute;
}
return base.GetUrl(content, mode, culture, current);
}
}
Set UrlMode for a single request
I am working on an API that allows users to request html of specific content so that it can be displayed in other applications within our company. The html can include images and media links. Is there a way to force all URLs to be rendered in absolute mode for those specific requests?
Currently, the only solution I can come up with is to change all of my partials so that they accept a parameter that will override the default URL mode every where I use the Url() extension. I would prefer to avoid this if possible.
I came up with a solution for anyone who finds this in the future. My solution involves creating 2 custom url providers that inherit the default providers; a provider inherited from DefaultUrlProvider and DefaultMediaUrlProvider. You need to register these providers with Umbraco builder in startup.
For my implementation, I look for a query string in the current Uri and then change the UrlMode to Absolute if the value is true. This results in every link and image to render with an absolute URL.
is working on a reply...