原因:类似于前边写的模板页,自己写了。还需要用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. java由字符型强制转化为整型例题

    此Java程序依次输出参数,参数类型为字符型,要求更改程序,使得字符型强制转化为整形,并将这些整数相加,最后输出总和. 原程序: package demo; public class CommandP ...

  2. VUE 计算属性 vs 侦听属性

    计算属性 vs 侦听属性 Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属性.当你有一些数据需要随着其它数据变动而变动时,你很容易滥用 watch——特别是如果你之前使用过 ...

  3. Linux grep命令使用方法

    Linux系统中grep命令可以根据指定的字符串或者正则表达式对文件内容进行匹配查找.在Linux文件处理和SHELL编程中使用广泛. grep基本语法 用法: grep [选项] "字符串 ...

  4. ios instancetype 和 id 的异同

    1.0 相同点:都可以作为方法的返回类型 2.0 不同点: a.instancetype 可以返回和方法所在类相同类型的对象   id 只能返回未知类型的对象 b. instancetype 只能作为 ...

  5. debug.keystare找不到的解决办法[转]

    重装系统之后,丢失了debug.keystore,找了很久都没有找到,根据网上所讲的只要重新运行一个android项目;就会在avd中生成一个新的debug.keystroe,此法也没解决,索性直接重 ...

  6. EF 控制code-first生成的数据库表名的单复数

    原地址:https://blog.csdn.net/winnyrain/article/details/51248410 在Code-First中,默认生成的数据库表的名称为类型的复数形式,如Mode ...

  7. 用JS和JQ来获取子节点!

    用JS和JQ来获取子节点!   在JS中,如果通过document.getElementsByTagName来获取子元素有个弊端:它不单会获取符合要求的子元素,就连同孙元素也会获取.如果有特殊要求,那 ...

  8. web service,soap ,http,tcp,udp

    webservice and soap  HTTP只负责把数据传送过去,不会管这个数据是XML.HTML.图片.文本文件或者别的什么.而SOAP协议则定义了怎么把一个对象变成XML文本,在远程如何调用 ...

  9. canvas画布内部重复画圆

    <!DOCTYPE html><html><head> <title>canvas example</title> <meta cha ...

  10. linux服务器设置只允许密钥登陆

    首先需要修改一些配置文件 vim /etc/ssh/sshd_config 进入sshd_config文件后需要更改几个地方 PubkeyAuthentication yes #启用公告密钥配对认证方 ...