Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Routing to default ('Index') action in page controllers

Vote:
 

Hello!

When visiting a page the default route seems to not be the 'index' action in the page controller but rather the first action in the code.

Let's take this controller as an example:

    public class EventPageController : ContentPageController<EventPage>
    {
        public IEnumerable<Event> GetEvents()
        {
            (...)
        }

        public ActionResult Index(EventPage currentContent)
        {
            (...)
        }
    }

When visiting '/events/random-event-page' path (which is associated with EventPage) it will display an array of events instead of routing to the Index action and render the view.
Reordering these actions 'solves' the issue - but it is rather covering it than actually solving it.

I was able to reproduce it in the Foundation solution as well if that helps (for the BlogItemPageController after moving GetTags method above Index)

Did anyone encounter the same issue? Is there any additional configuration that might affect this behavior?

Thanks!

#296722
Feb 17, 2023 16:51
Vote:
 

It sounds like you're hitting a similar issue to this one:

https://world.optimizely.com/forum/developer-forum/cms-12/thread-container/2022/11/migrating-partial-classes-of-same-controller-to-optimizely-12/

The issue seems to be that the route which would normally default to the index action is also matching the other action so, in the absence of any other means of prioritisation, the first action is being used. As I mentioned on that other post, if you can't rearrange the actions (or would prefer to have more control over priority), you can override the mechanism to select the appropriate action using an action constraint.

There may well be a way to resolve the issue by modifying the registered routes but, if there is, I've not worked it out.

#296723
Feb 17, 2023 18:06
Vote:
 

Thank you for your answer, Paul.

It's such a pity that it is not handled by the default Epi's matching policy.

Anyways, we decided that relying on the order of the methods is not really good idea as we will most likely forget about it after some time. We created custom action constraint based on your other post and automatically applied it to all `Index` methods in `PageController`s using `IActionModelConvention`. A little bit hacky but will satisfy our needs just fine.

#296863
Feb 20, 2023 13:17
* 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.