Try our conversational search powered by Generative AI!

CMS 12 - UrlResolver - routing for custom media type

Vote:
 

Hi folks,

I have a custom image type, for the sake of the example let's call it CustomImage inheriting ImageData.
The type is synchronized from external source, and has a separate repository descriptor CustomImageRepositoryDescriptor : ContentRepositoryDescriptorBase, which allows editors to drag and drop those images like regular uploaded media files into content areas, etc. 
The handling of returning proper file to client is done by endpoint

	[TemplateDescriptor(Inherited = true, TemplateTypeCategory = TemplateTypeCategories.HttpHandler)]
	public class CustomImageEndpoint : Endpoint, IRenderTemplate<CustomImage>
	{
		private static readonly ContentActionDescriptor _mediaContentActionDescriptor =
			new () { Inherited = true, ModelType = typeof(CustomImage) };

		private static readonly AuthorizeAttribute _authorizeAttribute = new (CmsPolicyNames.Read);
		private static readonly AuthorizeAttribute _previewAttribute = new (CmsPolicyNames.Preview);

		public CustomImageEndpoint(IBlobHttpHandler blobHttpHandler) : base(context => ProcessRequest(blobHttpHandler, context),
				new (_mediaContentActionDescriptor, _authorizeAttribute, _previewAttribute),
				nameof(CustomImageEndpoint))
		{
		}

		private static async Task ProcessRequest(IBlobHttpHandler blobHttpHandler, HttpContext context)
		{
			// Some more logic here
			await blobHttpHandler.Invoke(context);
		}
	}


The URL generated by following code

@{
    var urlResolver = ServiceLocator.Current.GetInstance<IUrlResolver>();
    var contentReference = new ContentReference(123);
}

<div>
    @urlResolver.GetUrl(contentReference)
</div>

looks something like this

/<language_code>/<repository_key>/<folder_name>/<file_name>

however, the CustomImage  is not culture specific type, and I would like to use following URL

/<repository_key>/<folder_name>/<file_name>

It's possible to remove the language code manually from generated URL, but then the URL works correctly only on Primary server, but not on Edit server (without language code on Edit it returns 404).

How can I make the IUrlResolver return URL without language code, that will work correctly both on Primary and Edit?

#287921
Edited, Sep 22, 2022 15:26
Vote:
 

I figured it out - the key was to register IContentRouteRegister with SupportMultiLanguage set to false.

public sealed class CustomImageRouteRegistration : IContentRouteRegister
{
	private readonly ContentRootService _contentRootService;

	public CustomImageRouteRegistration(ContentRootService contentRootService)
	{
		_contentRootService = contentRootService;
	}

	public ContentRouteDefinition ContentRouteDefintion => new ()
	{
		Name = nameof(CustomImageRouteRegistration),
		RouteRootResolver = _ => _contentRootService.Get(CustomImageRepositoryDescriptor.RepositoryKey),
		ContextMode = RouteContextMode.All,
		SupportMultiLanguage = false,
		StaticSegments = new[] { CustomImageRepositoryDescriptor.RepositoryKey.ToLower() },
	};
}
#287988
Edited, Sep 23, 2022 14:49
Quan Mai - Sep 24, 2022 19:39
Thanks for coming back to share your solution
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.