• Razor入门


    一、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)

  • 相关阅读:
    java基础知识(一)- 数据类型
    Java基础知识-去重
    java基础知识-冒泡排序
    Java排序方法sort的使用详解
    Java面试题-字符串操作
    Map.Entry遍历Map
    Java中String的用法
    Java循环控制语句-switch
    API文档打开显示'已取消到该网页的导航'的解决方法
    CentOS6.5 安装snort
  • 原文地址:https://www.cnblogs.com/genesis/p/4701378.html
Copyright © 2020-2023  润新知