How to render full RazorPage to string? - asp.net-core

I tried to use the solution from https://stackoverflow.com/a/54043063/234954, to render a page as a string (so I can turn it into a PDF), but that only gets me the main view, it doesn't get the layout associated with the page (and so it is missing the style sheets and some header/footers).
How can I render the full page as a string, and not just the partial view?

I used this article code in a recent project to render html to feed into PDF generation.
I used templates in the Views folder so not razor pages.
It works with Layouts and partials in templates.
https://ppolyzos.com/2016/09/09/asp-net-core-render-view-to-string/

I found that I could get what I wanted (the whole page) by making two changes to the answer I was looking at:
var view = new RazorView(_razorViewEngine,
_activator,
new List<IRazorPage>(),
page,
HtmlEncoder.Default,
new DiagnosticListener("ViewRenderService"));
changed to :
var view = new RazorView(_razorViewEngine,
_activator,
pageModel.PageContext.ViewStartFactories.Select(v => v()).ToList(),
page,
HtmlEncoder.Default,
new DiagnosticListener("ViewRenderService"));
and
await page.ExecuteAsync();
to
await view.RenderAsync(viewContext);
Note that if the viewstart pages aren't included in the view, then rendering the view produces the same as executing the page.

Related

JavaScript not executing after paginating through products in BigCommerce Cornerstone theme

I have a small bit of JavaScript which is not firing after navigating through paged results in a BigCommerce Cornerstone theme (using Cornerstone 6.7.0).
I've added the following to the onReady() method in the category.js file:
$('.mm-button').on('click', (e) => alert("clicked"));
This will execute on an initial page load but not after clicking on pagination links (next, prev, page number).
I've also tried moving the JavaScript into its own .js file and including it into the theme. The same behavior happens where it executes on the initial page load but not after paging through records.
The issue here is that pagination actually works by editing the content via JavaScript, so it does not run the category onReady() again. You could add the JavaScript into the page content function, but a simpler method is to simply delegate the click event. Change your code like this:
$('body').on('click', '.mm-button' (e) => alert("clicked"));

How to know at render time what partials are being used in a Razor page in ASPNET Core?

I have an ASPNET Core (.net6) project with a main page layout that automatically includes styles and scripts in the page bases on the page file name. Example: if you have a page called me/details (the file being me/details.cshtml) and there is a file called wwwroot/me/details.css, a reference to it will be included by my layout page inthe head of the html. The same happens for script files, but at the end of the body
I wanted to do the same for partials views. I want to include specific styles and script file references in the html for any partial views being rendered in the page, without extra code in the partials. Is there any way to know at render time, or at least at the middleware level, what partial views are being used in a page?

How to render multiple root elements in Xamarin forms?

I want to render two pages in Xamarin. One is the main page and the other one is a footer page- they both are .xaml files. The only problem is that the footer uses a ThriveGmbH.BottomNavigationBar.XF imported NuGet class and the Main Page uses a content page class. When I try to use both classes inside a single script I get a "multiple root elements" error. Is it possible to render both pages or call the footer page inside of the main page? Thank you in advance.

Accessing global site regions in Piranha CMS

I have added a global site region to my site and filled it with some content. How can I read this content from page view and/or layout?
This feature differs a bit between WebPages & MVC, the reason for this being that in WebPages (like WebForms) a Layout-page have a different model than the actual page being executed. If you use WebPages you simply add the following line first in the Layout page:
#inherits Piranha.WebPages.LayoutPage
This will automatically load the layout page model and all the global regions.
If you're using MVC this can't be done automatically as the Layout doesn't have a model. You can simply add the following in your Layout-page:
#{
Piranha.Models.PageModel global;
if (HttpContext.Current.Items["Piranha_CurrentPage"] != null) {
var current =
(Piranha.Models.Page)HttpContext.Current.Items["Piranha_CurrentPage"];
global = Piranha.Models.PageModel.GetBySite(current.SiteTreeId);
} else {
global = Piranha.Models.PageModel.GetBySite(Piranha.Config.SiteTreeId);
}
}
This snippet loads the layout page from:
If it's a page is displayed it loads the site tree the page is contained in
If it's a post it loads the site tree from the current site.
Hope this helps you!
Regards
/HÃ¥kan

Get the HTML output data from a wicket component

I'm currently writing a web widget, and I would like to fill the content of this widget with some HTML data generated by a wicket component on my server.
To do that, the server will output the HTML data via JSONP. So far so good.
However I need to get this HTML data. How can I get on the server the HTML output from some wicket component?
I dont know if this can be applied to your configuration, but I am using a view lines of code to retrieve rendered html which I wrote some time ago for building html based emails to be able to use wicket components in it
protected final String renderPage(Component page) {
final Response oldResponse = RequestCycle.get().getResponse();
BufferedWebResponse tempResponse = new BufferedWebResponse((WebResponse) RequestCycle.get().getOriginalResponse());
try {
RequestCycle.get().setResponse(tempResponse);
page.render();
}
finally {
RequestCycle.get().setResponse(oldResponse);
}
return tempResponse.toString();
}
As this rendering is made within an actual webapplication cycle but independently from the actual requestcycle, it is recommended to preserve the original requestcycle. The page will be rendered in your temporary webresponse from which you can retrieve the rendered html output.
Hope this may be what you are looking for
You might find everything you need in this Wicket Wiki article and the linked source code: Use wicket as template engine
Although I must admit that I never tried that, just read it and remembered for further reference...