作用:进一步将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. CM记录-操作系统调优

    1.避免使用swap分区---将hadoop守护进程的数据交换到磁盘的行为可能会导致操作超时:物理内存(交换)--Swap分区 2.调整内存分配策略---操作系统内核根据vm.overcommit_m ...

  2. 旅行商问题(TSP)、最长路径问题与哈密尔顿回路之间的联系(归约)

    一,旅行商问题与H回路的联系(H回路 定义为 哈密尔顿回路) 旅行商问题是希望售货员恰好访问每个城市一次,最终回到起始城市所用的费用最低,也即判断图中是否存在一个费用至多为K的回路.(K相当于图中顶点 ...

  3. AsciiMorph - 新奇的 ASCII 字符画生成工具&插件

    AsciiMorph 是一个新奇的 ASCII 字符画生成工具和开源插件.字符画(ASCII Art)的历史可以追溯到几十年前,起初是用在图形显示功能受限的设备上,用ASCII字符集里的可打印字符来拼 ...

  4. Redis 主从模式

    系统:Centos6.6x64安装目录:/usr/local/主:192.168.100.103从:192.168.100.104 ,下载安装: 安装依赖: # yum install gcc tcl ...

  5. Nginx PHP支持

    Nginx 支持 PHP server { server_name www.www1.com www1.com; location / { index index.php index.html; ro ...

  6. 表单之input的样式修改

    修改placeholder字体颜色 html5为input添加了原生的占位符属性placeholder,高级浏览器都支持这个属性,例如: <input type="text" ...

  7. Linux命令行与shell脚本编程大全.第3版(文字版) 超清文字-非扫描版 [免积分、免登录]

    此处免费下载,无需账号,无需登录,无需积分.收集自互联网,侵权通知删除. 点击下载:Linux命令行与shell脚本编程大全.第3版 (大小:约22M)

  8. 2017/05/04 java 基础 随笔

    1.java变量在使用之前必须初始化 int  a; a=10 ; int b; 没有初始化,也没有使用也不报错 2.强制类型转换  int a=8: byte b=6; b=(byte)(a+b); ...

  9. groovy与java中的多方法

    最近在学习groovy的时候从书里看到这么一个有趣的例子,刚开始我也猜错了: 最后测试答案是: son ----show 刚开始没理解,以为是num---show,但是后来看书上说是: test01接 ...

  10. Dubbo高可用

    高可用:通过设计减少系统不能提供服务的时间 (1).zookeeper宕机 原因:zookeeper宕机 现象:zookeeper注册中心宕机,还可以消费dubbo暴露的服务. 健壮性: 监控中心宕掉 ...