D
D
Dmitry Petrov2018-06-19 01:53:22
ASP.NET
Dmitry Petrov, 2018-06-19 01:53:22

Is there a mechanism to bundle-nga the contents of script/style tags for asp.net mvc?

ASP.NET 4.5 introduced the ability to create script bundles using ScriptBundle/@Scripts.Render. But this feature only allows you to merge existing .js files. But what if I have complex templates, each of which can have its own logic for building the html structure and its own script tag with certain content inside? Moreover, this script tag contains inside a line of c# code (conditions, variables, etc.), i.e. formed dynamically. As a result, a complete document could contain 4-5 script tags with a lot of logic, which is not good - it's a complete mess in terms of document structure. Ideally, all such content should be moved to the end of the document, or even to a separate file (preferably one), but how?
Yes, you can create your own separate js file for each view and partial view (in which the script sections of each view are placed), you can even generate such js files in a separate action if you need to execute the razor logic, and then get the combined bundle on the page, but this also looks very ugly.
It would be great if in any view you could write @Scripts.Include("~scripts", script.../script), and in the main layout just insert @Scripts.Render("~scripts"), getting some address to a temporary file with all the contents of the script tags of all views and partial views involved in building the page. Moreover, yes, it is clear that not all scripts need to be included in such a temporary file, because some of them may change on a per-page basis, and some may be semi-static, so it's sufficient to leave out @Scripts.Include such content.
I rummaged through the net and did not find such an opportunity in the standard mechanism. Maybe there are some packages that extend the standard behavior?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor P., 2018-06-28
@Jeer

Hi, no one writes anything at all (
The meaning of bundles is to combine existing scripts into one and place this one file on the server, correct me if I'm wrong. That is, in debug mode, you have available files, and in release mode, these files are first glued together on the server, and then ready-made files are given away. but it should be just so and this is a fundamental point.
If you have dynamically created scripts (when you form a layout with js interspersed with the help of a break), then it turns out that for each request of each user you have your own, different files. Dynamically created pages are served to the user, but nothing remains on the server. If for each request the process of linking and minification first goes through, then it will be unrealistically long. Doing this once and then giving everyone one cached file is fast. And for each request to do this, it will be unrealistic.
In general, the answer to your question is that this mechanism does not allow you to add dynamically generated scripts to bundles.
In fact, if it is impossible for you to separate the js code into separate files, then something is wrong with the structure of the project and the js code in it.
Try (for self-development purposes) to see how your problems are solved in other projects. For example, in the studio there is a .net core + angular template, webpack is already configured in it, which performs approximately the same function as bundles (but it will also not cope with your problem).
The point is that you first create a script from c # that is immediately filled with initialized values, but when you update (I think you update via ajax?), you have methods that receive data via api and update the same values ​​on the page, according to the essence of the code becomes, if not twice, then one and a half more. In spa solutions, only work through api is left, that is, there will be no dynamic scripts, so all such js code remains static, it can be linked and minified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question