菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
1
0

Razor入门

原创
05/13 14:22
阅读数 44845

一、Razor简介
Razor不是编程语言,它是一种允许您向网页中嵌入基于服务器的代码的标记语法,也就是可以在html网页中嵌入的写入C#代码,Razor在VS中有自动提示,使用起来会方便一点,如下代码,循环输出i的值

<ul>
  @{
    for(int i=0;i<10;i++)
    {
      <li>@i</li>
    }
  }
</ul>

当网页被写入浏览器时,基于服务器的代码能够创建动态内容。在网页加载时,服务器在向浏览器返回页面之前,会执行页面内的基于服务器代码。由于是在服务器上运行,这种代码能执行复杂的任务,比如访问数据库。
Razor基于ASP.NET,它为web应用程序的创建而设计。它拥有传统 ASP.NET 标记的能力,但更易使用,也更易学习。

二、Razor工作流程
1.Razor读取cshtml文件(string cshtml = Razor.Parse(html, new { Name = name, Age = age });)
2.Razor会根据cshtml内容动态的生成C#代码
3.执行C#代码形成一个程序集
4.通过反射的方式执行程序集形成html代码

三、Razor使用方法(用ashx一般处理程序输出cshtml内容)
因为Razor大多是在MVC中使用,要想脱离MVC使用Razor,可以借助开源的RazorEngine.dll,就可以在控制台、winform窗体、ASP.NET中使用。
1.引用RazorEngine.dll并导入命名空间using RazorEngine;
//读取文件路径
string fillPath = context.Server.MapPath("~/Text1.cshtml");
//读取文件内容
string html = File.ReadAllText(fillPath);
//调用Razor的方法Parse
string cshtml = Razor.Parse(html, new { Name = name, Age = age });
//输出
context.Response.Write(cshtml);
2.在cshtml中调用服务器的变量
1)首先在服务器中将变量定义为全局静态的变量,如:
public static string name;
public static List<string> list = new List<string>();
2)然后在服务器的ProcessRequest方法中设置变量的值,如:
name="abcd";
list.RemoveRange(0, list.Count);//先清空,否则在客户端获取时会重复出现
list.Add("aaa");list.Add("bbb");list.Add("ccc");
3)最后在cshtml获取变量值时,先最顶部导入命名空间,再获取,如:
@using WebApplication2 //导入变量所在的命名空间,前面加@,写在最顶部
@{string str = Text1.name;} //接收变量值,用@{...},变量值写在大括号里面,变量名前面需要加上类名
//那么value="@str"就是将变量的值写入文本框中
<input type="text" name="name" value="@str" />
//如下为接收List集合

@{
  List<string> listCount = Text1.list;
}
//输出list值
for(int i=0;i<listCount.Count;i++)
{
  <li>@listCount[i]</li>
}

//获取类中的属性值
C#中代码:
public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

string name = "syfpc";
int age = 8;
string cshtml = Razor.Parse(html, new { Name = name, Age = age });

cshtml代码:
@Model.Name
@Model.Age

//封装一个ParseRazor方法

public static string ParseRazor(HttpContext context, string cshtmlPath, object model)
{
    string fullPath = context.Server.MapPath(cshtmlPath);
    string cshtml = File.ReadAllText(fullPath);
    string cachName = fullPath + File.GetLastWriteTime(fullPath);
    string html = Razor.Parse(cshtml, model, cachName);
    return html;
}

//生成一个文本框

public static RawString Text2()
{
    RawString rawstring = new RawString("<input type='text' />");
    return rawstring;
}

//在cshtml中调用此方法
@ProccessHelp.Text2()

//生成一个单选按钮

public static RawString CheckBox(string name, string id, bool ischecked)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<input type='checkbox' name='" + name + "' id='" + id + "'");
    if (ischecked)
    {
        sb.Append("checked='checked'");
    }
    sb.Append("/>");
    RawString rawstring = new RawString(sb.ToString());
    return rawstring;
}

//在cshtml中调用此方法
@ProccessHelp.CheckBox("check","check",true)

发表评论

0/200
1 点赞
0 评论
收藏