Asp.net dynamically generates JS files

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.