作用:进一步将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. javascript 利用冒泡机制显示与隐藏模态框

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

  2. AbstractQueuedSynchronizer同步队列与Condition等待队列协同机制

    概要: AQS维护了一个同步队列 Condition是JUC的一个接口,AQS的ConditionObject实现了这个接口,维护了一个等待队列(等待signal信号的队列) 线程调用reentran ...

  3. SNMP收集

    http://velep.com/archives/416.html     协议基本格式

  4. codeforces724G Xor-matic Number of the Graph

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. luogu P1053 篝火晚会

    传送门 首先如果题目的目标状态不是一个环就不合法 然后先把这个环搞出来,然后每个位置上的数对这个数对应的位置连边,可以发现有若干个环,而只要对这些环执行操作就好了,答案上界显然是\(n\).然后,如果 ...

  6. HDU小小练

    hdu1253胜利大逃亡(bfs) 题意:就是城堡问题,找出可通行路径即可 思路:三维BFS,设定前后上下左右6个方向搜索,注意开始的时候人站的位置可以是墙. hdu1495非常可乐(bfs) 题意: ...

  7. jzoj4313 电话线铺设(最小生成树+最近公共祖先)

    题面 \(solution:\) 这道题很奇妙,需要对kruskal重构树有足够的了解!我们先对王牌电缆实行kruskal重构树,然后我们再来枚举每一条李牌电缆,我们将某一条李牌电缆加进这棵树中必然构 ...

  8. postman变量的使用和设置

    之前只是使用postman做接口管理——将各个项目使用到的接口分类管理起来,用的时候手动改参数调用.这次项目连着跑三个接口,需要用到前一个接口的参数,还来回切平台,真的很麻烦,所以就搜了一下有什么便利 ...

  9. Django学习手册 - 模板语言(前端获取后台数据)

    先在views视图内,定义列表数据,以及字典数据.运用render函数传递两个列表数据至前端. from django.shortcuts import render list_info = [ {& ...

  10. layer弹出层的iframe页面回调

    $("#ChoiceBank").click(function () { var width = $("#content").css("Width&q ...