Provides asp.net to generate core Static Page code

Source: Internet
Author: User

  Bkjia.com provides asp.net to generate core Static Page code.

Reference content is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Using System. Net;
Using System. Text. RegularExpressions;

Namespace Zxq. Common
{
// Zheng xiqiang
// Www.cnblogs.com/zhengxiqiang
// Generate static page operations
// 2009.2
Public class ToHtml
{
# Region read Template
/// <Summary>
/// Read the Template
/// </Summary>
/// <Param name = "templateUrl"> template address </param>
/// <Param name = "coding"> encoding </param>
/// <Returns> template content </returns>
Public string ReadTemplate (string templateUrl, Encoding code)
{
String tlPath = System. Web. HttpContext. Current. Server. MapPath (templateUrl );
StreamReader sr = null;
String str = null;
// Read the template content
Try
{
Sr = new StreamReader (tlPath, code );
Str = sr. ReadToEnd ();
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
Sr. Close ();
}
Return str;
}
# Endregion

# Region file generation
/// <Summary>
/// Generate a file
/// </Summary>
/// <Param name = "str"> file content </param>
/// <Param name = "htmlFile"> file storage address </param>
/// <Param name = "fileName"> file name </param>
/// <Param name = "coding"> encoding </param>
/// <Returns> file name </returns>
Public bool CreateHtml (string str, string htmlFile, string fileName, Encoding code)
{
StreamWriter sw = null;
Bool a = false;
// Write generation
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;
}
# Endregion

# Region determine whether a file exists
/// <Summary>
/// Determine whether a file exists
/// </Summary>
/// <Param name = "FilePath"> file path </param>
/// <Returns> </returns>
Public bool FileExists (string FilePath)
{
If (System. IO. File. Exists (FilePath ))
Return true;
Else
Return false;
}
# Endregion

# Region create directory
/// <Summary>
/// Create a directory
/// </Summary>
/// <Param name = "Path"> </param>
/// <Returns> </returns>
Public bool FolderCreate (string Path)
{
// Determine whether the target directory exists. If it does not exist, create it.
If (! FolderExists (Path ))
Directory. CreateDirectory (Path );
Return FolderExists (Path );
}
# Endregion

# Region determine whether a directory exists
/// <Summary>
/// Determine whether a directory exists
/// </Summary>
/// <Param name = "Path"> Path </param>
/// <Returns> </returns>
Public bool FolderExists (string Path)
{
If (Directory. Exists (Path ))
Return true;
Else
Return false;
}
# Endregion

# Region static list page
/// <Summary>
/// Static list page
/// </Summary>
/// <Param name = "pageCount"> total number of pages </param>
/// <Param name = "currentPage"> current page </param>
/// <Param name = "prefix"> for example, list </param>
/// <Param name = "suffix"> example:. shtml </param>
/// <Returns> </returns>
Public string GetHtmlPager (int pageCount, int currentPage, string prefix, string suffix)
{
Int stepNum = 4;
Int pageRoot = 1;
PageCount = 0? 1: pageCount;
CurrentPage = 0? 1: currentPage;
StringBuilder sb = new StringBuilder ();
Sb. Append ("<ul> ");
Sb. append ("<li class = pagerTitle> & nbsp; pagination & 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 = 'homepage'> Home </a> & nbsp; </li> \ r ");
Sb. append ("<li> & nbsp; <a href = '" + prefix + Convert. toString (currentPage-1) + suffix + "'title = 'previous page'> previous page </a> & nbsp; </li> \ r ");
}
}
Else
{
Sb. append ("<li> & nbsp; <a href = '" + prefix + "1" + suffix + "'title = 'homepage'> Home </a> & nbsp; </li> ");
Sb. append ("<li> & nbsp; <a href = '" + prefix + Convert. toString (currentPage-1) + suffix + "'title = 'previous page'> previous page </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 = 'nth" + I. toString () + "page"> "+ 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 = 'Next page'> next page </a> & nbsp; </li> \ r ");
Sb. append ("<li> & nbsp; <a href = '" + prefix + pageCount. toString () + suffix + "'title = 'tail'> tail' </a> & nbsp; </li> \ r ");
}
}
Else
{
Sb. append ("<li> & nbsp; <a href = '" + prefix + Convert. toString (currentPage + 1) + suffix + "'title = 'Next page'> next page </a> & nbsp; </li> \ r ");
Sb. append ("<li> & nbsp; <a href = '" + prefix + pageCount. toString () + suffix + "'title = 'tail'> tail' </a> & nbsp; </li> \ r ");
}
Sb. Append ("</ul> ");
Return sb. ToString ();
}
# Endregion

# Region compressing Html files
/// <Summary>
/// Compress Html files
/// </Summary>
/// <Param name = "html"> Html file </param>
/// <Returns> </returns>
Public string ZipHtml (string Html)
{
Html = Regex. Replace (Html, @ "> \ s +? <","> <"); // Removes spaces in 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 retrieves text from HTML, and retains br, p, and img
/// <Summary>
/// Obtain the text from HTML, and retain the br, p, and img
/// </Summary>
/// <Param name = "Html"> text content </param>
/// <Returns> </returns>
Public string GetTextFromHtml (string Html)
{
Regex regEx = new Regex (@ "</? (?! Br | /? P | img) [^>] *> ", RegexOptions. IgnoreCase );
Return regEx. Replace (Html ,"");
}
# Endregion
}
}

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.