作用:进一步将HTML代码和C#代码进行解耦

1.1 引用程序集(RazorEngine.dll,System.Web.Razor.dll)

  1.1.1 可以从http://razorengine.codeplex.com/ 上下载

  1.1.2 也可以NuGet程序包安装(建议使用第一种,因为这个安装完成后只有System.Web.Razor.dll这一个,)

1.2 Parse的使用

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
姓名: <b>@Model.Name</b>
<!--姓名2: <b>@Model.Name</b>-->
 
</br>
年龄:<b>@Model.Age</b> </body>
</html>

html

using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Razor;
namespace _04_RazorTest
{
/// <summary>
/// _01_Parse的使用 的摘要说明
/// </summary>
public class _01_Parse的使用 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//01获取要想要使用的HTML
string html = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+"01-Parse的使用.html"); html = Razor.Parse(html, new { Name="张三",Age =});
context.Response.Write(html);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

1.3 通过Razor还可以直接在html中写C#代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
@{
for(int i=;i<Model;i++){
if(i%==)
{
<b>@i</b>
}
} }
</body>
</html>

html

using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Razor;
namespace _04_RazorTest
{
/// <summary>
/// _01_Parse的使用 的摘要说明
/// </summary>
public class _02_Parse的使用_2 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//01获取要想要使用的HTML
string html = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+"02-Parse的使用-2.html"); html = Razor.Parse(html, );
context.Response.Write(html);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

1.4 类似母版页

using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace _04_RazorTest
{
/// <summary>
/// _03_类似母版页 的摘要说明
/// </summary>
public class _03_类似母版页 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//01 获取HTML页
string html = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+"03-类似母版页.html");
string top = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+"03-Top.html");
//02 编译 替换
Razor.Compile( top,"Top");
//03 必须先Compile,再Parse
html= Razor.Parse(html, new {Name = "张三" });
context.Response.Write(html);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
@Include("Top")
<hr />
姓名: <b>@Model.Name</b>
</body>
</html>

03-类似母版页.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<b>这是标题行</b>
</body>
</html>

Top.html

1.5 对Razor的封装

1.5.1 正常不使用Razor的代码实现

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<table >
<thead><th>新闻</th><th>类型</th></thead>
$content
</table>
</body>
</html>

html

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web; namespace _03_新闻
{
/// <summary>
/// NewsIndex 的摘要说明
/// </summary>
public class NewsIndex : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string sql = "select * from TypeInfo";
DataTable dt = SqlHelper.ExecuteTable(sql);
int count = dt.Rows.Count;
StringBuilder sb = new StringBuilder();
if (count > )
{
foreach (DataRow dr in dt.Rows)
{
sb.Append("<tr>");
sb.Append("<td>");
sb.Append(dr["TypeTitle"]);
sb.Append("</td>");
sb.Append("<td>");
sb.Append(dr["TypeParentId"]);
sb.Append("</td>");
sb.Append("</tr>");
}
}
else {
sb.Append("<tr>");
sb.Append("<td>");
sb.Append("没有数据");
sb.Append("</td>");
sb.Append("</tr>");
} string html = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"NewsIndex.html"));
html = html.Replace("$content",sb.ToString());
context.Response.Write(html);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

ashx

1.5.2 通过Razor的代码实现

1.5.2.1 封装RazorHelper类

using RazorEngine;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web; namespace _03_新闻
{
public static class RazorHelper
{
#region 01 渲染
public static void Render(string modelName ,object obj,params IncludeType[] ps)
{ #region 01Compile
//01-01 首先进行Compile -- Razor.Compile(top,"Top");
//--需要两个参数,所以封装一个内部类,
//--由于传递来的个数不确定,所以使用params
if (ps.Length > )
{
int count = ps.Length;
for (int i = ; i < count; i++)
{
IncludeType t1 = ps[i];
string temp;
//确定是文件
if (t1.IsFile)
{
temp = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "View\\" + t1.Content);
}
else
{
temp = t1.Content;
}
//关键一步
Razor.Compile(temp, t1.Title);
}
}
#endregion #region 02 Parse
// html= Razor.Parse(html, new {Name = "张三" });
string html = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+"View\\"+modelName);
html = Razor.Parse(html,obj); #endregion #region 03 输出响应流
HttpContext.Current.Response.Write(html);
#endregion
}
#endregion
#region 02 定义一个母版页的类型
/// <summary>
/// 定义一个母版页的类型 Razor.Compile(Content,Title)时候用
/// </summary>
public class IncludeType
{
//02-01 定义标题Razor.Compile(Content,Title);
public string Title { get; set; }
//02-02 定义内容
public string Content { get; set; }
//02-03 定义是否是文件(文件,字符串)
public bool IsFile { get; set; } }
#endregion
}
}

RazorHelper

<!DOCTYPE html>
@using System.Data; <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<table>
<thead>
<th>新闻</th>
<th>类型</th>
</thead>
@{
DataTable dt = Model as DataTable; <!--这一句代码很重要-->
if(dt.Rows.Count >){
int count = dt.Rows.Count;
for(int i=;i<count;i++)
{
<tr>
<td> @dt.Rows[i]["TypeTitle"]</td>
<td>@dt.Rows[i]["TypeParentId"]</td>
</tr>
} }else
{
<tr>
<td>没有数据,</td>
</tr>
}
}
</table>
</body>
</html>

html

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web; namespace _03_新闻
{
/// <summary>
/// NewsRazor 的摘要说明
/// </summary>
public class NewsRazor : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string sql = "select * from TypeInfo";
DataTable dt = SqlHelper.ExecuteTable(sql);
int count = dt.Rows.Count;
RazorHelper.Render("ModelNewsShow.html", dt);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

NewsRazor

1.5.2.2 进一步完善--母版页

  只需要改3处

  01-添加Top页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<a href="#">添加</a>
&nbsp; &nbsp; &nbsp; &nbsp;
<a href="#">修改</a>
</body>
</html>

Top.html

  02- 修改html页面  

<!DOCTYPE html>
@using System.Data; <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
@Include("Top")
<br />
<table>
<thead>
<th>新闻</th>
<th>类型</th>
</thead>
@{
DataTable dt = Model as DataTable; <!--这一句代码很重要-->
if(dt.Rows.Count >){
int count = dt.Rows.Count;
for(int i=;i<count;i++)
{
<tr>
<td> @dt.Rows[i]["TypeTitle"]</td>
<td>@dt.Rows[i]["TypeParentId"]</td>
</tr>
} }else
{
<tr>
<td>没有数据,</td>
</tr>
}
}
</table>
</body>
</html>

html

  @Include("Top")

  03-修改NewRazor.ashx调用处

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web; namespace _03_新闻
{
/// <summary>
/// NewsRazor 的摘要说明
/// </summary>
public class NewsRazor : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string sql = "select * from TypeInfo";
DataTable dt = SqlHelper.ExecuteTable(sql);
//int count = dt.Rows.Count;
RazorHelper.Render("ModelNewsShow.html", dt, new RazorHelper.IncludeType { Title ="Top",Content="Top.html",IsFile = true});
} public bool IsReusable
{
get
{
return false;
}
}
}
}

NewsRazor

  RazorHelper.Render("ModelNewsShow.html", dt, new RazorHelper.IncludeType { Title ="Top",Content="Top.html",IsFile = true});
   

步步为营-69-Razor基础的更多相关文章

  1. Razor基础语法简介

    http://blog.csdn.net/pasic/article/details/7072340 Razor的出现,使页面看起更加简洁,Razor的页面后缀为:.cshtml Razor基础语法: ...

  2. Razor基础语法

    原文:Razor基础语法 一.介绍      Razor是ASP.NET MVC视图引擎的代号,支持.cshtml和.vbhtml两种模板文件,符号@标记服务端代码的开始,比以前asp.net的< ...

  3. Razor基础语法一

    目录: 什么是Razor? 渲染HTML Razor语法 隐式 Razor 表达式 显式 Razor 表达式 什么是Razor? Razor是基于服务端代码转换成网页的标记语法.语法主要包括Razor ...

  4. MVC Razor基础

    @ 可以编写一条C#语句@{} 可以编写一组C#语句@: 将文字内容直接输出到页面上去@() 在一句中将一段C#代码包括起来,证明这一句完整的C#代码 Razor解决路径问题: Html.Action ...

  5. 搜索引擎 ElasticSearch 之 步步为营2 【基础概念】

    在正式学习 ElasticSearch 之前,首先看一下 ElasticSearch 中的基本概念. 这些概念将在以后的章节中出现多次,所以花15分钟理解一下是非常值得的. 英文好的同学,请直接移步官 ...

  6. Razor基础,视图里如何调用controller里的函数

    1.单行代码书写 @代码 2.多行代码书写 @{ //@加个大括号就可以在里面写C#代码了. C#代码第一行 C#代码第二行 } 3.Razor模板引擎会自动判别代码块,但是当代码存在二义性的时候,可 ...

  7. day 69 Django基础五之django模型层(一)单表操作

    Django基础五之django模型层(一)单表操作   本节目录 一 ORM简介 二 单表操作 三 章节作业 四 xxx 一 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现 ...

  8. 步步为营-35-SQL语言基础

    SQL 结构化查询语言(Structured Query Language) DDL DML DCL 在此不再对其进行详细解释 1 DDL 数据库定义语言 1.1 创建数据库脚本 --DDL crea ...

  9. Blazor 003 : Razor的基础语法

    上文,我们通过剖析一个最简单的 Blazor WASM 项目,讲明白了 Razor 文件是什么,以及它被转译成 C#后长什么样子.也介绍了 Razor 中最简单的一个语法:Razor Expressi ...

  10. UI设计师零基础入门到精通精品视频教程【155课高清完整版】

    [福吧资源网分享]课程是非常完整的,也是非常零基础的,适合任何学员,有需要的可以下载看看!课程目录:第1章 Adobe Photoshop CS6课时1 Adobe Photoshop CS6入门基础 ...

随机推荐

  1. xen 配置vm 跟随xen server一起启动

    查看Xen Server 信息 (1)查看pool信息 [root@xenserver-243 ~]# xe pool-list uuid ( RO) : e29037aa-0dca-f95a-193 ...

  2. javascript 模拟日历

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Bellman-Ford算法:POJ No.3169 Layout 差分约束

    #define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...

  4. 求FIRST集和FOLLOW集

    花了点时间弄了个大概,希望对和我一样的人有所帮助.   文法如下: E -> TE'E' -> +TE'|εT -> FT'T' -> *FT'|εF -> (E)|id ...

  5. SSH 互信

    A:master B:server 在A B机器 当前用户的绝对目录 进行创建 .ssh  # mkdir ~/.ssh# chmod 700 .ssh/# /usr/bin/ssh-keygen - ...

  6. 列式数据库~clickhouse 副本集架构的搭建

    clickhouse 搭建副本集 一 原理:  1 依赖ZK,ZK的基础上,ZK存储数据库元数据 2  使用复制表引擎创建复制表,包括ZK路径和副本名,相同ZK路径的表可以相互复制 3  复制表本身拥 ...

  7. mysql 原理 ~ double write

    一 简介:今天来聊聊double write 二 细节 1 Double write 是InnoDB在 tablespace(ibdata1)上的128个页(2个区)是2MB: 2 何谓页断裂 所谓页 ...

  8. Activity,Fragment的状态保存

    http://blog.csdn.net/hqdoremi/article/details/26376797 https://blog.csdn.net/u013588712/article/deta ...

  9. Maven入门-依赖管理(Jar包管理)(二)

    1       依赖管理(Jar包管理) 1.添加依赖  

  10. ubuntu 14.04 安装 OpenCV -2.4.13

    1. 安装 (1) 更新软件源 sudo apt-get update sudo apt-get upgrade (2)删除以前安装的 FFMPEG 和 x264 库: sudo apt-get re ...