Dynamic generation of JS files is often used as a cache technology. In addition, it is supported by pure Client technology on various platforms, and it is convenient to reference it. In Asp.net, it is actually very easy to dynamically generate JS files. For example, a questionnaire is used in multiple places on the website. At this time, you can consider generating a JS file. Facilitate maintenance and management.
Protected override void render (htmltextwriter writer)
{
Int titleid = 0;
Stringwriter html = new stringwriter ();
System. Web. UI. htmltextwriter Tw = new system. Web. UI. htmltextwriter (HTML );
Base. Render (TW );
Streamwriter SW;
String dir = server. mappath ("~ /JS/ask /");
If (! Directory. exists (DIR ))
{
Directory. createdirectory (DIR );
}
String Path = dir + "Ask" + "_" + titleid + ". js ";
Sw = new streamwriter (path, false, system. Text. encoding. utf8 );
String newhtml = html. tostring (). Replace ("\" "," "). Replace (" \ r \ n ","");
String lasthtml = "document. Write (\" "+ newhtml + "\")";
Sw. Write (lasthtml. tostring ());
Sw. Close ();
Tw. Close ();
}
Not to mention how to reference it.