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
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?
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); ....
see http://joelabrahamsson.com/building-a-pdf-channel-for-episerver-7/. It might not be a direct help but may be useful.
Either send in content link from view to action or use PageRouteHelper class:
var pageRouteHelper = ServiceLocator.Current.GetInstance<PageRouteHelper>();
var contentLink = pageRouteHelper.Page.ContentLink;
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>
What about something like:
<a href="@Url.Action("UrlAsPDF", "ControllerName", new { contentLink = Model.CurrentPage.ContentLink} )">Download UrlAsPDF</a>
public ActionResult UrlAsPDF(ContentReference contentLink) {
....
}
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 :)
I want to create a pdf from the url using rotativa.
In the view,
Download UrlAsPDF
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?