AI OnAI Off
You can try to use PageRouteHelper class to get current page as follows:
var pageRouteHelper = ServiceLocation.ServiceLocator.Current.GetInstance<PageRouteHelper>();
if (pageRouteHelper.Page != null)
{
// do the stuff
}
Is it what you need?
Do you need the whole PageData object in the MainManu index action? If not, it could be a better idea to only send the data that you need to that controller, like: @Html.Action("Index", "MainMenu", layoutModel).
Sorry for the delayed answer on this, thanks Dmytro that was exactly what I was looking for.
I injected the PageRouteHelper in the controllers constructor like this:
public class MainMenuController : Controller
{
private PageRouteHelper pageRouteHelper;
public MainMenuController(PageRouteHelper pageRouteHelper)
{
this.pageRouteHelper = pageRouteHelper;
}
}
When I have a controller for a typed page template for example:
I can get the CurrentPage reference in an action method like this:
But if I for example in my _Layout.cshtml use this syntax, to call another Controller/Action
The MainMenuController is defined like this:
Then currentPage will end up to be null, this worked in a previous CTP version of CMS 7 so I suspect this might be as designed?
The question is how can I get hold of currentPage in this scenario?
I have tried to convert the current url to a PageReference with both PageReference.ParseUrl and UrlRewriteprovider.RewriteToInternal but it did not work. I also found a workaround to send the PageReference from the _Layout.cshtml directly to the Controller/Action in the RouteData-values. But did not feel like the best way to do it as I had to write this into the cshtml file.
Obviously there must be a better way?