How to get CurrentPage in a MVC Controller

Vote:
 

When I have a controller for a typed page template for example:

public class StartPageController : PageController<StartPage>

I can get the CurrentPage reference in an action method like this:

public ActionResult Index(StartPage currentPage)

But if I for example in my _Layout.cshtml use this syntax, to call another Controller/Action

@Html.Action("Index", "MainMenu")

The MainMenuController is defined like this:

public class MainMenuController : Controller
{
  public ActionResult Index(PageData currentPage)
  {
  }
}

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.

@Html.Action("Index", "MainMenu", new { currentPageRef = ViewContext.RouteData.DataTokens[EPiServer.Web.Routing.RoutingConstants.NodeKey] })

Obviously there must be a better way?

#62800
Nov 02, 2012 16:15
Vote:
 

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?    

#62807
Nov 02, 2012 21:09
Vote:
 

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).

#62852
Nov 05, 2012 13:44
Vote:
 

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

    

#63439
Nov 20, 2012 9:28
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.