How to make delayed helper render?

RickettsialPox 1 Reputation point
2021-03-13T16:10:36.007+00:00

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.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,280 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yihui Sun-MSFT 801 Reputation points
    2021-03-15T08:20:01.893+00:00

    Hi @RickettsialPox ,

    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