question

RickettsialPox-2569 avatar image
0 Votes"
RickettsialPox-2569 asked ·

How to make delayed helper render?

We have a lot of small JS script or HTML utilities inside thousands of small-big (Razor) helpers, that we are using in the rather complex web application. One view can be in first situation loaded as Html.Partial from other site, but also can be recall for reload directly from a page. Also this view can be called in reload partialview - page with more partials to reload at once. Some of pages can have Layout (some from ViewStart), but also can be generated without Layout or PartialViewResult (using just View method at ActionResult of controller). So it is really big problem to determine first page, last page, partial of partial or partial of main page. And of course Controller method can be called A, but contains View call on B.cshtml.

And from now one we want to generate all small scripts, which causing slow generation and pageload, at one place delayed after all partial and layout pages are generated. We made delayed stack on model (all our models have one ancestor). But how to manage recall delayed code somewhere in main (first) view? It is no problem to make .Render call in every our layout page - if it is possible to determine non-layout direct partialview call. For direct partialview I made _Nothing layout with render call, that render dleayed code for this partialview. (I've tried some determine of partial - but it have problem with Html.Partial call. In helper I am filling .Layout property automaticaly - to make .Render call from _Nothing layout.) For layout of layout I need really determine mainpage - to not render code early (in one of partial), but also render at all ... The best solution will be some output stream I think ...

Thank you.

dotnet-csharpdotnet-aspnet-mvc
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YihuiSun-MSFT avatar image
0 Votes"
YihuiSun-MSFT answered ·

Hi @RickettsialPox-2569,

For layout of layout I need really determine mainpage - to not render code early (in one of partial), but also render at all ...

Is the problem you are encountering about the loading order of multiple layouts?For example: you want to render view B in view A, and both A and B use different layout views. You want the layout view in B to be loaded first, but eventually all the layout views that are used will be loaded.If this is the case, you need to know:

➤Code that needs to run before each view or page should be placed in the _ViewStart.cshtml file.
1. The statements listed in _ViewStart.cshtml are run before every full view ( not layouts, and not partial views).
2. In my opinion, all layout views are only rendered when they are in use.

By convention, the _ViewStart.cshtml file is located in the Views folder of the MVC project. It can also be created in all other Views sub-folders.
- For example, the following _ViewStart.cshtml in the Test99 folder sets the Layout property to _Layoutnew.cshtml. So now, Index.cshtml will display in the _Layoutnew.cshtml instead of default _Layout.cshml.
77722-image.png


And from now one we want to generate all small scripts, which causing slow generation and pageload, at one place delayed after all partial and layout pages are generated.

➤In addition, if you have a lot of scripts to load, you can use Bundling and minification.
1. They are two techniques you can use to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)
2. You can click this link to see more explanation.


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
YihuiSun


image.png (12.8 KiB)
· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sadly that is the problem. We are using ViewStart for default layout as I mentioned. So obviously I've tried to use it for default render place, as I wrote. The problem is, that ViewStart is not called on partialveiw called via PartialViewResult. If you use View call, than it is called, but not for partial view. (That I really tested.) I have two problems. Layout of layout as you mentioned, but also one layout/main page can have multiple Html.Partial calls. And I have no other code, than stack fill in partial and render in some of layouts. There are layouts, that never should contains delayed code and some layouts are dialog content. That means, that some pages, some layouts are used in dialog, and some as full page. I need to render delayed JS before every partial render, because until now we had JS loaded immediately after helper and I cannot destroly whole system by render code after partials, that contains custom JS calling not-generated JS methods.

0 Votes 0 ·

So I need to always have layout, that make Render call - for non-layout pages I mange to make _Nothing layout, that cointains only Render and is given automatically by stack filling method - if that method determine "this is one page partial - no other pages will be or was generated." Problem is, that I cannot manually add .Render to every our of milions .cshtml ... I need some event, or output stream to put it when we are using some of delayed helpers ... ViewStart was tested, that is not called 100 % ... And some pages may not have any layout ... Also I am affraid of custom layout - in some of areas ... Best solution will be automatical render at end of stream - or something like that ... I need to declare partial JS generated methods before partial pages, so partial pages can call them ...

0 Votes 0 ·