Try our conversational search powered by Generative AI!

Replacing IUrlResolver

Vote:
 

I am trying to create a custom Url Resolver. I have seen several implementations online that use a similar method to this:

public void ConfigureContainer(ServiceConfigurationContext context)

    {

        context.Container.Configure(

            x =>

            {

                x.For<UrlResolver>().Singleton().Use<MyUrlResolver>();

            });

    }

But this is not how I am implementing di, and this even an out of date way of doing di. I am using what is the default di, which is basically Microsoft's di. So, in that case, how do I replace the url resolver?

Is is just Services.AddSingleton<IUrlResolver,MyUrlResolver>()?

#258478
Jul 05, 2021 14:14
Vote:
 

Hi, very helpful documentation: Dependency injection | Optimizely Developer Community (episerver.com)

With IUrlResolver, then I prefer to use

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class CustomUrlResolverConfigurableModule : IConfigurableModule
    {
        public void ConfigureContainer(ServiceConfigurationContext context)
        {
            context.ConfigurationComplete += (o, e) =>
            {
                e.Services.Intercept<UrlResolver>((locator, currentResolver) =>
                    new CustomUrlResolverInterceptor(
                        currentResolver
                        , locator.GetInstance<ISiteDefinitionResolver>()
                        , locator.GetInstance<RouteCollection>()
                        , locator.GetInstance<IContentLoader>()
                        , locator.GetInstance<ISiteDefinitionRepository>()
                        , locator.GetInstance<TemplateResolver>()
                        , locator.GetInstance<IPermanentLinkMapper>()
                        , locator.GetInstance<IContentLanguageSettingsHandler>()
                        , locator.GetInstance<IContentUrlCache>()
                        , locator.GetInstance<ISynchronizedObjectInstanceCache>()
                        , locator.GetInstance<IContextModeResolver>()
                        , locator.GetInstance<IRequestHostResolver>()));
            };
        }

        public void Initialize(InitializationEngine context)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
        }
#258483
Jul 05, 2021 20:24
Vote:
 

Hi Ethan, your code only says "if someone asks for UrlResolver then give them MyUrlResolver", you should also say the same for the interface IUrlResolver as we usually ask for the interface and not a concreate type.

So to fix, also add the same registration for the interface IUrlResolver with your MyUrlResolver implementation, so then any code that asks for UrlResolver or IUrlResolver for DI will get your MyUrlResolver implementation.

#258564
Jul 07, 2021 9:03
Vote:
 

And just read well your question, is this question now related to the Optimizely CMS netcore-preview and you now want to register a custom implementation of IUrlResolver?

If so, as the default MS DI is used, then you can refer to the MS documentation on how to register services. Optimizely CMS NET Core preview DI documentation.

So your code is showing the "old way", so in the NET core (or 5) you would register your service in the startup class ConfigureServices method as shown here.

#258565
Jul 07, 2021 9:19
Vote:
 

Antti

Thanks for the heads up on also replacing the IUrlResolver. Ill do that.

Not for the .Net Core preview. This site will launch just after .Net Core version is supposed to launch. Too soon after such a huge upgrade. But thanks for the tip.

#258568
Jul 07, 2021 12:21
Vote:
 

public void ConfigureContainer(ServiceConfigurationContext context)
        {
            var services = context.Services;

            services.AddSingleton<IUrlResolver, MyUrlResolver>();

is the right way to do if you want to replace the entire default implementation. If you only want to change one or two methods in the default implementation, you can use Intercept as Ha's answer, but I guess that is not your intention

#258576
Edited, Jul 07, 2021 19:32
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.