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

Page routing question

Vote:
 

I have a page called MyPage used in the controller MyPageController : PageController<MyPage>. I want to inherit a child controller from MyController like MyChildPageController : MyPageController and want the routing to land in MyChildPageController code. Is it possible? If yes, can you please tell me how to do that? As the controller is selected by CMS now based on MyPage, it's unpredictable which controller will be chosen. It sometimes is using MyPageController and sometimes MyChildPageController. I am using CMS 12.15.1.0, .NET 6. Thanks in advance.

#303180
Jun 08, 2023 15:36
Vote:
 

Hmm. That's a bit of a strange scenario. My first thought would be that, if you're not going to use "MyPageController" directly and you always want to use "MyChildPageController", you could just mark "MyPageController" as abstract. If that's not an option, I suspect you could make an explicit selection of the controller to use by registering it using an instance of IViewTemplateModelRegistrator.

#303182
Jun 08, 2023 17:39
Vote:
 

Thanks Paul. I looked at IViewTemplateModelRegistrator and I see that it let me choose the partial. In my case, I want to choose child controller because there is business logic that is different from the parent class. We have similar functionalities for multiple pages. I am trying to build common functionalities in the parent and derive the child from the parent in special cases. Not sure it is supported. It will be great to hear alternative approaches if there are any.

#303185
Jun 08, 2023 20:53
Vote:
 

You can create a seperate class for you buisness logic, say MyPageViewModelFactory and MyChildPageViewModelFactory and inject both classes in constructor of your MyPageController and choose according to your buisness logic which factory class you want to access. And if you want different View at both place then you could that too there. I think this would be better approach if you only want a different buisness logic.

#303372
Edited, Jun 12, 2023 8:17
Vote:
 

This worked.

[TemplateDescriptor(Name = "MyPageController ", Tags =new string[] {"Parent"})]
MyPageController : PageController<MyPage>

{
...
}

[TemplateDescriptor(Name = "MyChildPageController ", Tags =new string[] {"Child"})]
MyChildPageController : MyPageController

{
...
}

Implement ITemplateResolverEvents

Add the below code to the event callback function

        private void OnTemplateResolving(object sender, TemplateResolverEventArgs eventArgs)
        {
            var render = eventArgs.SupportedTemplates
                .SingleOrDefault(r => r.Tags?.FirstOrDefault()?.ToLower().Contains("child") == true &&
                r.TemplateTypeCategory == eventArgs.SelectedTemplate.TemplateTypeCategory);

            if (render != null)
            {
                eventArgs.SelectedTemplate = render;
            }
        }


#303390
Jun 12, 2023 13:17
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.