ASP. NET template engine uses Razor to generate HTML static pages

Source: Internet
Author: User

I didn't think it was easy at first, after I finished it.

This is divided into several steps

1. Get page Template html

2. Get Data

3. Parsing templates and data, generating static page HTML code

4. Generating static files

The template form is the MVC pattern, and MVC looks at it.

Mainly 2nd and 3rd steps.

You need to apply the following file

RazorEngine.dll

System.Web.Razor.dll

<summary>
Get HTML code for a page
</summary>
<param name= "url" > template page Path </param>
<param name= "Encoding" > page encoding </param>
<returns></returns>
public string gethtml (string url, System.Text.Encoding Encoding)
{
byte[] buf = new WebClient (). Downloaddata (URL);
if (encoding! = NULL) return encoding. GetString (BUF);
String html = System.Text.Encoding.UTF8.GetString (BUF);
encoding = getencoding (HTML);
if (encoding = = NULL | | encoding = = SYSTEM.TEXT.ENCODING.UTF8) return HTML;
return encoding. GetString (BUF);
}
<summary>
Get the encoding of the page
</summary>
<param name= "html" >html source </param>
<returns></returns>
Public System.Text.Encoding getencoding (string html)
{
String pattern = @ "(? i) \bcharset= (? <charset>[-a-za-z_0-9]+)";
String charset = Regex.match (HTML, pattern). Groups["CharSet"]. Value;
try {return System.Text.Encoding.GetEncoding (charset);}
catch (ArgumentException) {return null;}
}

<summary>
Create a static file
</summary>
<param name= "Result" >html code </param>
<param name= "Createpath" > Build path </param>
<returns></returns>
public bool Createfilehtmlbytemp (string result, string createpath)
{


if (!string. IsNullOrEmpty (Result))
{


if (string. IsNullOrEmpty (Createpath))
//{
Createpath = "/default.html";
//}
string filepath = Createpath. Substring (Createpath. LastIndexOf (@ "\"));
Createpath = Createpath. Substring (0, Createpath. LastIndexOf (@ "\"));
if (! Directory.Exists (Createpath))
//{
Directory.CreateDirectory (Createpath);
//}
Createpath = Createpath + filepath;

Try
{
FileStream FS2 = new FileStream (Createpath, FileMode.Create);
StreamWriter SW = new StreamWriter (FS2, new System.Text.UTF8Encoding (false));//Remove UTF-8 BOM
Sw. Write (result);
Sw. Close ();
FS2. Close ();
FS2. Dispose ();
return true;
}
catch {return false;}
}
return false;
}

Main part

1. Data

var getjosn = new Dataname () {Name = "Here is the article title", ThemeName = "<span style=\" color:red;\ "> here is the article content </span>"};

var entity = new Dataname ();//entity


This part is to note that the entity class

Reflection part

Type Typet = Getjosn.gettype ();
Type Typeen = Entity.gettype ();

system.reflection.propertyinfo[] Propertyinfost = Typet.getproperties ();
foreach (System.Reflection.PropertyInfo propertyinfot in Propertyinfost)
{
System.Reflection.PropertyInfo Propertyinfoen = Typeen.getproperty (propertyinfot.name);
if (Propertyinfoen! = null && propertyinfot.getvalue (GETJOSN, NULL)! = NULL)
{
Propertyinfoen.setvalue (Entity, Propertyinfot.getvalue (getjosn, NULL), NULL);
}
}

HTML page is code

<body style= "color:aqua;font-size:30px;" >
@Model. Name
@Model. ThemeName
</body>


Parse templates generate static page HTML code
result = Razor.parse ("Template Hmtl", Entity);//entity Corpse Data

I can see the main part basically.

ASP. NET template engine uses Razor to generate HTML static pages

Related Article

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.