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.
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.
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.
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;
}
}
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.