软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP.NET > 提供asp.net生成静态页核心代码

提供asp.net生成静态页核心代码

  烈火建站学院(LieHuo.Net)讯 提供asp.net生成静态页核心代码

以下为引用的内容:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace Zxq.Common
{
//郑希强
//www.cnblogs.com/zhengxiqiang
//生成静态页操作
//2009.2
public class ToHtml
{
#region 读取模板
/// <summary>
/// 读取模板
/// </summary>
/// <param name="templateUrl">模板地址</param>
/// <param name="coding">编码</param>
/// <returns>模板内容</returns>
public string ReadTemplate(string templateUrl, Encoding code)
{
string tlPath = System.Web.HttpContext.Current.Server.MapPath(templateUrl);
StreamReader sr = null;
string str = null;
//读取模板内容
try
{
sr = new StreamReader(tlPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
return str;
}
#endregion

#region 生成文件
/// <summary>
/// 生成文件
/// </summary>
/// <param name="str">文件内容</param>
/// <param name="htmlFile">文件存放地址</param>
/// <param name="fileName">文件名</param>
/// <param name="coding">编码</param>
/// <returns>文件名</returns>
public bool CreateHtml(string str, string htmlFile, string fileName, Encoding code)
{
StreamWriter sw = null;
bool a = false;
//写入生成
try
{
htmlFile = System.Web.HttpContext.Current.Server.MapPath(htmlFile);
this.FolderCreate(htmlFile);
sw = new StreamWriter(htmlFile + fileName, false, code);
sw.Write(str);
sw.Flush();
a = FileExists(htmlFile + fileName);
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
}
return a;
}
#endregion

#region 判断文件是否存在
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <returns></returns>
public bool FileExists(string FilePath)
{
if (System.IO.File.Exists(FilePath))
return true;
else
return false;
}
#endregion

#region 创建目录
/// <summary>
/// 创建目录
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public bool FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!FolderExists(Path))
Directory.CreateDirectory(Path);
return FolderExists(Path);
}
#endregion

#region 判断目录是否存在
/// <summary>
/// 判断目录是否存在
/// </summary>
/// <param name="Path">路径</param>
/// <returns></returns>
public bool FolderExists(string Path)
{
if (Directory.Exists(Path))
return true;
else
return false;
}
#endregion

#region 静态列表页分页
/// <summary>
/// 静态列表页分页
/// </summary>
/// <param name="pageCount">总页数</param>
/// <param name="currentPage">当前页</param>
/// <param name="prefix">如:list</param>
/// <param name="suffix">如:.shtml</param>
/// <returns></returns>
public string GetHtmlPager(int pageCount, int currentPage, string prefix, string suffix)
{
int stepNum = 4;
int pageRoot = 1;
pageCount = pageCount == 0 ? 1 : pageCount;
currentPage = currentPage == 0 ? 1 : currentPage;
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
sb.Append("<li class=pagerTitle>&nbsp;分页&nbsp;" + currentPage.ToString() + "/" + pageCount.ToString() + "&nbsp;</li>\r");
if (currentPage - stepNum < 2)
pageRoot = 1;
else
pageRoot = currentPage - stepNum;
int pageFoot = pageCount;
if (currentPage + stepNum >= pageCount)
pageFoot = pageCount;
else
pageFoot = currentPage + stepNum;
if (pageRoot == 1)
{
if (currentPage > 1)
{
sb.Append("<li>&nbsp;<a href='" + prefix + "1" + suffix + "' title='首页'>首页</a>&nbsp;</li>\r");
sb.Append("<li>&nbsp;<a href='" + prefix + Convert.ToString(currentPage - 1) + suffix + "' title='上页'>上页</a>&nbsp;</li>\r");
}
}
else
{
sb.Append("<li>&nbsp;<a href='" + prefix + "1" + suffix + "' title='首页'>首页</a>&nbsp;</li>");
sb.Append("<li>&nbsp;<a href='" + prefix + Convert.ToString(currentPage - 1) + suffix + "' title='上页'>上页</a>&nbsp;</li>\r");
}
for (int i = pageRoot; i <= pageFoot; i++)
{
if (i == currentPage)
{
sb.Append("<li class='current'>&nbsp;" + i.ToString() + "&nbsp;</li>\r");
}
else
{
sb.Append("<li>&nbsp;<a href='" + prefix + i.ToString() + suffix + "' title='第" + i.ToString() + "页'>" + i.ToString() + "</a>&nbsp;</li>\r");
}
if (i == pageCount)
break;
}
if (pageFoot == pageCount)
{
if (pageCount > currentPage)
{
sb.Append("<li>&nbsp;<a href='" + prefix + Convert.ToString(currentPage + 1) + suffix + "' title='下页'>下页</a>&nbsp;</li>\r");
sb.Append("<li>&nbsp;<a href='" + prefix + pageCount.ToString() + suffix + "' title='尾页'>尾页</a>&nbsp;</li>\r");
}
}
else
{
sb.Append("<li>&nbsp;<a href='" + prefix + Convert.ToString(currentPage + 1) + suffix + "' title='下页'>下页</a>&nbsp;</li>\r");
sb.Append("<li>&nbsp;<a href='" + prefix + pageCount.ToString() + suffix + "' title='尾页'>尾页</a>&nbsp;</li>\r");
}
sb.Append("</ul>");
return sb.ToString();
}
#endregion

#region 压缩Html文件
/// <summary>
/// 压缩Html文件
/// </summary>
/// <param name="html">Html文件</param>
/// <returns></returns>
public string ZipHtml(string Html)
{
Html = Regex.Replace(Html, @">\s+?<", "><");//去除Html中的空白字符.
Html = Regex.Replace(Html, @"\r\n\s*", "");
Html = Regex.Replace(Html, @"<body([\s|\S]*?)>([\s|\S]*?)</body>", @"<body$1>$2</body>", RegexOptions.IgnoreCase);
return Html;
}
#endregion

#region 从HTML中获取文本,保留br,p,img
/// <summary>
/// 从HTML中获取文本,保留br,p,img
/// </summary>
/// <param name="Html">文本内容</param>
/// <returns></returns>
public string GetTextFromHtml(string Html)
{
Regex regEx = new Regex(@"</?(?!br|/?p|img)[^>]*>", RegexOptions.IgnoreCase);
return regEx.Replace(Html, "");
}
#endregion
}
}

    相关阅读
    网友评论
    栏目导航
    推荐软件