原因:类似于前边写的模板页,自己写了。还需要用replace来替换成自己想要的变量。。

常见的模板引擎:Razor、Nvelocity、Vtemplate。 Razor有VS自动提示,而且有助于学习asp.net mvc。(Nvelocity、Vtemplate自行学习)

1.Nvelocity。Vemplate 语法在C#中没有自动提示。但是用着非常方便的

2.借助于开源的RazorEngine,我们可以在非asp.net mvc项目中使用Razor引擎,甚至在控制台、WinForm项目中都可以使用Razor(自己开发代码生成器)

3.在非mvc项目中创建Razor文件(cshtml ,可以利用自动提示)的方法,新建一个html,改名为cshtml。(需要重新打开,才有智能提示)

4.Razor中@后面跟表达式表示在这个位置输出表达式的值,模板中Model为传递给模板的对象。

5.@{}中为C#代码,C#代码还可以和html代码混排

6.由于不是在MVC项目中,所以无法使用@Html.DropDownList、@Url.Encode()等。

RazorEngine(c#语言写的)是微软做的一个开源的模板引擎,不是简单的在asp.net MVC中用,其他地方也是可以使用的。

自己写个cshtml 步骤: 
1。项目名字–右键—添加—新建–Razor.cshtml会有自动提示的。(推荐这种用法)

<!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>
<ul>
@{ for (int i = 0; i < 10; i++)
{
<li>@i</li>;
}
} </ul> </body>
</html>

  

2。添加对RazorEngine的引用(1.放到项目的lib文件夹中,2.右键–引用–添加引用–浏览—打开该项目的lib文件,选择RazorEngine.dll文件即可!)

3。添加一般处理程序Razor1.ashx

using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace Web2
{
/// <summary>
/// Razor 的摘要说明
/// </summary>
public class Razor1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";//1。修改为html文本类型
//2.获取模板页的路径
string fullPath = context.Server.MapPath("~/Razor.cshtml"); //3.读取出模板页中的内容
string cshtml = File.ReadAllText(fullPath); //4.使用Razo的Parse方法转化为html文本信息
string html = Razor.Parse(cshtml); //5.输出到浏览器端
context.Response.Write(html);
} public bool IsReusable
{
get
{
return false;
}
}
}
}

  

修改Razor也可以读取“类”中数据,“数据库中的字段”

using RazorEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web; namespace Web2
{
/// <summary>
/// Razor 的摘要说明
/// </summary>
public class Razor1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";//1。修改为html文本类型
//2.获取模板页的路径
string fullPath = context.Server.MapPath("~/Razor.cshtml"); //3.读取出模板页中的内容
string cshtml = File.ReadAllText(fullPath); /*
//4.使用Razo的Parse方法转化为html文本信息
string html = Razor.Parse(cshtml);
*/ //拓展:怎么将变量传递到模板页cshtml中呢?
//使用Razor的第二个方法
//假设从数据库中读取的变量 name, age /*
int age = 9;
string name = "rupeng";
//使用匿名类
//var model = new { Age=age,Name=name };
//string html = Razor.Parse(cshtml, model); //简化写
string html = Razor.Parse(cshtml, new { Age=age,Name=name}); */
Dog dog = new Dog();
dog.Id = 18;
dog.Name = "哮天犬";
string html = Razor.Parse(cshtml,dog); //5.输出到浏览器端
context.Response.Write(html);
} public bool IsReusable
{
get
{
return false;
}
}
} public class Dog
{
public int Id{ get; set; }
public string Name { get; set; }
} }

  

效果

 

为什么使用Reazor的更多相关文章

随机推荐

  1. Spring/SpringBoot定义统一异常错误码返回

    配置 大致说下流程, 首先我们自定义一个自己的异常类CustomException,继承RuntimeException.再写一个异常管理类ExceptionManager,用来抛出自定义的异常. 然 ...

  2. 转: python requests的安装与简单运用

    requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢? 官方文档中是这样说明的: python的标准库urlli ...

  3. ArcGIS模型构建器案例教程-批量复制工作空间所有要素类

    ArcGIS模型构建器案例教程-批量复制工作空间所有要素类 目的:批量复制工作空间所有要素类 工具名称:WorkspaceCopyFeatureClasses 使用方法:输入工作空间,指定输出工作空间 ...

  4. ubuntu 下安装和启动SSH 服务

    安装OPENSSH 服务端 sudo apt-get install openssh-server 查看进程是否启动 ps -e | grep ssh 删除密钥文件 rm /etc/ssh/ssh_h ...

  5. 学习JS的心路历程-参数传递方式(上)

    很多人认为JS的传递方式是值是Call by value, 物件及数组是Call by Reference.甚至还有人宣称其实JS是Call by sharing,那到底是哪一个呢? 这两天我们一一来 ...

  6. ssl 的jks 生成工具

    https://www.myssl.cn/tools/merge-jks-cert.html 通过key 私钥 ,和公钥pem 生成jks

  7. 项目IDEA启动配置

    在所有java启动项中加入 -Djute.maxbuffer=2048000 tomcat 在catalina.bat 中第一行加入 set JAVA_OPTS=-Djute.maxbuffer=20 ...

  8. 对于“2017面向对象程序设计(JAVA)第四周学习总结”存在问题的反馈

    对于“2017面向对象程序设计(JAVA)第四周学习总结”存在问题的反馈 “这部分同学博文总结没有写,实验作业没有提交.”——1.关于博文作业.实验作业教学功能的正解:学习知识.暴露问题.衔接课上.2 ...

  9. Java中字节流和字符流复制文件

    字节流和字符流复制文件的过程: 1.建立两个流对象 绑定数据源和目的地 2.遍历出需复制的文件写入复制过后的新文件中(只不过是遍历的时候是区分字节和字符的) 3.访问结束后关闭资源 字节流复制文件: ...

  10. JMeter学习(二十四)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults(转载)

    转载自 http://www.cnblogs.com/yangxia-test Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Autho ...