asp.net动态生成js文件


动态生成js文件常常被作为缓存技术的一种被使用。还有就是它是纯客户端技术在各个平台都支持,引用它也比较方便。在asp.net中想动态生成js文件其实很简单。比如要将一个调查问卷在网站中多处被使用。这时候就可以考虑,把它生成一个js文件。利于维护和管理。

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();

}

如何引用就不用说了。

优质内容筛选与推荐>>
1、NVelocity入门,为服务器与客户端传输xml数据,实现Ajax通信铺平道路[转]
2、Android获取屏幕大小和设置无标题栏
3、SQL2005-使用openrowset 里读取excel文件
4、开源gis学习资料
5、【转】Attribute在.NET编程中的应用(五)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号