Image URL Resolver Plugin
To implement a custom image URL resolver for the CMS Frontend Server, inherit from the IImageUrlResolverPlugin interface and implement the ResolveUrl method. This allows you to provide custom logic for resolving image URLs based on the file context.
Interface
The IImageUrlResolverPlugin interface is distributed in the NuGet package MediaiBox.Cms.FrontEnd.Model.
public interface IImageUrlResolverPlugin : ICustomBehavior
{
public Task<UrlResolutionResult> ResolveUrl(FileContext context, CancellationToken cancellationToken);
}
public record FileContext
{
public int MibObjectId { get; init; }
public string MediaType { get; init; }
public string PageKey { get; init; }
public string TemplateComponentKey { get; init; }
public IReadOnlyDictionary<string, object> Fields { get; init; }
}
FileContext Properties
- MibObjectId: The unique identifier of the object.
- MediaType: The media type of the file.
- PageKey: The key of the page where the image is used.
- TemplateComponentKey: The key of the template component.
- Fields: Fields from the Mib Object
Example Implementation
Below is an example of a custom image URL resolver plugin. This implementation returns a sample image URL for odd MibObjectId values and signals it cannot handle even ones.
using MediaiBox.Cms.FrontEnd.Model.Plugins.ImageUrlResolver;
namespace MibCustomComponent.Sample.ImageUrlResolver;
public class CustomImageUrlResolverPlugin : IImageUrlResolverPlugin
{
public Task<UrlResolutionResult> ResolveUrl(FileContext context, CancellationToken cancellationToken)
{
if (context.MibObjectId % 2 == 0)
{
return Task.FromResult(UrlResolutionResult.CannotHandle());
}
return Task.FromResult(UrlResolutionResult.FromUrl(
"https://samples.mediaibox.com.br/MibAuth/ux/img/logo/agile-full-logo.png"));
}
}
Registering the Plugin
To enable your plugin in MibServer3, add the following environment variables to your configuration:
MIBCMSFRONTENDSERVERBASECONFIG_IMAGEURLRESOLVERPLUGIN_ASSEMBLYNAME={AssemblyName}
MIBCMSFRONTENDSERVERBASECONFIG_IMAGEURLRESOLVERPLUGIN_CLASSNAME={ClassName}
- ASSEMBLYNAME: The name of the assembly containing your plugin class (e.g.
MibCustomComponent.Sample) - CLASSNAME: The fully qualified class name of your plugin implementation (e.g.
MibCustomComponent.Sample.ImageUrlResolver.CustomImageUrlResolverPlugin)
See the Dependency Injection section.