November Happy Hour will be moved to Thursday December 5th.

Use own urlresolver?

Vote:
 

Is it even possible to override EPiServer UrlResolver? 

I get an error message saying "Only concrete types can be built by ConstructorInstance"

Anyone done this? 

/Jens

#173418
Dec 23, 2016 10:20
Vote:
 

Hi Jens,

Yes, it is possible.

EPiServer.Web.Routing.UrlResolver is an abstract class, and episerver's default implementation is EPiServer.Web.Routing.Internal.DefaultUrlResolver.

That class is decorated with ServiceConfiguration attribute like this:

[ServiceConfiguration(Lifecycle = ServiceInstanceScope.Singleton, ServiceType = typeof (UrlResolver))]
public class DefaultUrlResolver : UrlResolver
{
	...
}

You can create your own implementation

public class MyUrlResolver : UrlResolver
{
	...
}

And tell EPiServer to use your implementation like this:

[ModuleDependency(typeof(ServiceContainerInitialization))]
public class MyUrlResolverInitModule : IConfigurableModule
{
    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        context.Container.Configure(
            x =>
            {
                x.For<UrlResolver>().Singleton().Use<MyUrlResolver>();
            });
    }

    public void Initialize(InitializationEngine context)
    {
    }

    public void Uninitialize(InitializationEngine context)
    {
    }
}

Implementing all UrlResolver's methods can be quite tricky, so if you just want to test if your IoC setup works, your can implement MyUrlResolver like this:

public class MyUrlResolver : DefaultUrlResolver
{
    public MyUrlResolver(
        RouteCollection routes,
        IContentLoader contentLoader,
        ISiteDefinitionRepository siteDefinitionRepository,
        TemplateResolver templateResolver,
        IPermanentLinkMapper permanentLinkMapper,
        IContentLanguageSettingsHandler contentLanguageSettingsHandler)
        : base(
            routes,
            contentLoader,
            siteDefinitionRepository,
            templateResolver,
            permanentLinkMapper,
            contentLanguageSettingsHandler)
    {
    }
}

Inject UrlResolver in your page controller and check if MyUrlResolver or DefaultUrlResolver is injected.

#173419
Dec 23, 2016 11:09
* 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.