作用:进一步将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. 绕过/*,web.xml直接访问jsp【转】

    web.xml中如果配置了/* 全匹配,那么不能用servet去响应页面返回了,因为全都被会/*拦截. <servlet> <servlet-name>validateAuth ...

  2. Javaweb学习笔记——(四)——————JavaScript基础&DOM目录

    1.案例一:在末尾添加节点 第一个:获取到ul标签 第二部:创建li标签 document.createElement("标签名称")方法 第三步:创建文本 document.cr ...

  3. OptionParser命令参数介绍及使用

    使用optionParse解析命令行参数分以下几个步骤: 创建parser实例 使用add_option添加我们要处理的命令行参数 得到解析sys.argv后的options对象,查看用户的输入 代码 ...

  4. luogu P3522 [POI2011]TEM-Temperature

    这道题暴力做法就是枚举每个起点,然后向后拓展到不能拓展 就像这样(红框是每个位置的取值范围,绿线是你取的值构成的折线) 应该可以发现,左端点往右移的过程中,右端点也只能不动或往右移,所以我们可以每次移 ...

  5. HDU4738 Caocao's Bridges【强连通】

    题意: 曹操有N个岛,这些岛用M座桥连接起来,每座桥有士兵把守(也可能没有),周瑜想让这N个岛不连通,但只能炸掉一座桥,并且炸掉一座桥需要派出不小于守桥士兵数的人去,桥的守兵数为0时,也需要派出一个人 ...

  6. JavaScript学习 - 基础(六) - DOM基础操作

    DOM: DOM定义了访问HTML 和XML 文档的标准:1.核心DOM 针对结构化文档的标准模型2.XMK DOM 针对XML文档的标准模型3.HTML DOM 针对HTML文档的标准模型 DOM节 ...

  7. Jquery对当前日期的操作(格式化当前日期)

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  8. android 服务解析

    https://blog.csdn.net/luoyanglizi/article/details/51586437 2.service和Thread的区别 定义上: thread是程序运行的最小单元 ...

  9. SVG2PNG(前台和后台将SVG转换为PNG)--amcharts导出png

    在项目中用到了amcharts,amcharts图标统计插件是利用SVG实现的,其自带下载png功能,但是不支持IE以下浏览器.因此研究了SVG转换为png,最终实现的效果是将amcharts生成一张 ...

  10. Freemarker导出word的简单使用

    1.模板 username:${username} password:${password} <#list mylist as item> ${item.name!} ${item.pas ...