zoukankan      html  css  js  c++  java
  • ASP.NET页面生成HTML

    网上看到很多人问NET生成HTML的相关技术,我以前也曾为这个问题伤脑筋很久,网上的很多是模版替换

    字符的方法来实现,在实际的应用中,那样的方法根本就没有用。其实生成HTML的原理就是读取已经存在

    的网站的HTML代码,再保存为其他文件,命名为****.htm或****.html。我自己写了一个类,使用很简单

    ,比较容易懂,也希望能给大家一点启示。

    类名为schtml.cs

     1using System; 
     2using System.Data; 
     3using System.Configuration; 
     4using System.Web; 
     5using System.Web.Security; 
     6using System.Web.UI; 
     7using System.Web.UI.HtmlControls; 
     8using System.Web.UI.WebControls; 
     9using System.Web.UI.WebControls.WebParts; 
    10using System.Web.Hosting; 
    11using System.IO; 
    12using System.Text; 
    13using System.Net; 
    14
    15/// <summary> 
    16///schtml 的摘要说明 
    17/// </summary> 

    18public class schtml 
    19
    20    public schtml() 
    21    
    22        // 
    23        //TODO: 在此处添加构造函数逻辑 
    24        // 
    25    }
     
    26
    27
    28    //生成html 
    29    public void creathtml(string url,string path) 
    30    
    31        Encoding code = Encoding.GetEncoding("utf-8"); 
    32        StreamReader sr = null
    33        StreamWriter sw = null
    34        string str = null
    35         
    36        //读取远程路径 
    37        WebRequest temp = WebRequest.Create("http://" + HttpContext.Current.Request.Url.Host.ToString() + url); 
    38        WebResponse myTemp = temp.GetResponse(); 
    39        sr = new StreamReader(myTemp.GetResponseStream(), code); 
    40        //读取 
    41        try 
    42        
    43            sr = new StreamReader(myTemp.GetResponseStream(), code); 
    44            str = sr.ReadToEnd(); 
    45
    46        }
     
    47        catch (Exception ex) 
    48        
    49            throw ex; 
    50        }
     
    51        finally 
    52        
    53            sr.Close(); 
    54        }
     
    55
    56        //写入 
    57        try 
    58        
    59            sw = new StreamWriter(path, false, code); 
    60            sw.Write(str); 
    61            sw.Flush(); 
    62
    63        }
     
    64        catch (Exception ex) 
    65        
    66            throw ex; 
    67        }
     
    68        finally 
    69        
    70            sw.Close(); 
    71
    72        }
     
    73
    74    }
     
    75}

    76

    使用方法也很简单

    1 schtml html1 = new schtml(); 
    2  html1.creathtml("/product/default.aspx", Server.MapPath("http://www.cnblogs.com/product/"+ "index.htm");

    html1.creathtml的两个参数说明一下:

    前面一个是页面路径/product/default.aspx

    后面一个Server.MapPath("http://www.cnblogs.com/product/") + "index.htm",是指要把生成的文件保存在哪个文件夹,此处是保存在product目录下,文件名为index.html

     

     

    可以相互交流相互学习

  • 相关阅读:
    【zz】编程修养(一二三)
    Lec1计算字符串的相似度
    ASP.NETFLV处理流代码
    获取指定文件夹下所有子目录及文件(树形)
    Flex及AS3的百条常用知识(转载)
    [AS3] 解决跨域问题
    Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
    ASP.NET中的File类和Directory类的相关知识
    Asp.net 备份、还原Ms SQLServer及压缩Access数据库
    http://blog.csdn.net/octverve/archive/2008/01/29/2071356.aspx
  • 原文地址:https://www.cnblogs.com/joylee/p/1311846.html
Copyright © 2011-2022 走看看