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

how to extract the url of the page

Vote:
 

I want to create a pdf from the url using rotativa.

In the view,

In the controller, Iam trying to extract the url of the page as follows:
[HttpGet]
public ActionResult UrlAsPDF()
{
var urlResolver = ServiceLocator.Current.GetInstance();
var pageUrl = urlResolver.GetVirtualPath(this.CurrentPage.ContentLink);

....
}
I am getting the error as 'Object reference not set to an instance of an object'.

What changes do I need to make to the view/controller to get the url of the page it's called from?

#148755
May 23, 2016 13:59
Vote:
 

Looks pretty good. You should be using the UrlResolver. I'm not sure if your code for current page is working though...check that current page isn't null

#148757
May 23, 2016 14:27
Vote:
 

Hi Daniel,

When I debug the code, this.CurrentPage.ContentLink is having a null value. Do I need to pass any other parameters to the ActionResult to get the CurrentPage?

Is 'this.CurrentPage.ContentLink' the right parameter to pass in?

#148758
May 23, 2016 14:49
Vote:
 

check out the example code of alloy...normally you need a parameter called currentPage (or currentBlock) of the correct page type in the method. 

  public class StartPageController : PageControllerBase<StartPage>
    {
        public ActionResult Index(StartPage currentPage)
        {
            var model = PageViewModel.Create(currentPage);
           ....
#148759
Edited, May 23, 2016 15:30
Vote:
 

see http://joelabrahamsson.com/building-a-pdf-channel-for-episerver-7/. It might not be a direct help but may be useful.

#148760
May 23, 2016 15:31
Vote:
 

Either send in content link from view to action or use PageRouteHelper class:

var pageRouteHelper = ServiceLocator.Current.GetInstance<PageRouteHelper>();
var contentLink = pageRouteHelper.Page.ContentLink;

#148762
May 23, 2016 15:44
Vote:
 

Below is my controller. I could see the values for currentPage in the Index action method but not in the UrlAsPDF method. I don't know why.

[HttpGet]
public ActionResult Index(Answers currentPage)
{
var model = new AnswerViewModel(currentPage, SiteSettings);
return View(model);
}

[HttpGet]
public ActionResult UrlAsPDF(Answers currentPage)
{
var url = ServiceLocator.Current.GetInstance<UrlResolver>();
var pageUrl = url.GetVirtualPath(currentPage.ContentLink);

//var url = ContentReferenceExtension.GetUrl(currentPage.ContentLink);
return Content(url.ToString());
}

Here is my View, Is this right way of calling Controller action method with parameters in EPiServer

<div>
<a href="@Url.Action("UrlAsPDF", "ControllerName")">Download UrlAsPDF</a>
</div>

#148763
May 23, 2016 15:46
Vote:
 

What about something like:

<a href="@Url.Action("UrlAsPDF", "ControllerName", new { contentLink = Model.CurrentPage.ContentLink} )">Download UrlAsPDF</a>

public ActionResult UrlAsPDF(ContentReference contentLink) {

....

}

#148768
May 23, 2016 15:57
Vote:
 

You're getting a null reference exception because you're not sending the currentPage value from the client side.

Controller:

[HttpGet]
public ActionResult UrlAsPDF(ContentReference contentLink)
{
    var url = // return URL based on content link
    return Content(url.ToString());
}

View:

<div>
    <a href="@Url.Action("UrlAsPDF", "ControllerName", new { contentLink = Model.CurrentPage.ContentLink })">Download UrlAsPDF</a>
</div>

Just check if routing works :)

edit: Tim was faster :)

#148769
Edited, May 23, 2016 15:59
* 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.