Razor语法和Razor引擎大全

一、Razor语法

1、Razor的标识符

         解释:@字符被定义为Razor服务器代码块的标识符,后面的表示是服务器代码了。web form中使用<%%>中写服务器代码一个道理。在vs工具里面提供了代码着色和智能感应的功能。

 
  1. @{

  2. string userName = "启超";

  3. <span>我的名字叫:@userName</span>

  4. <span>我的出生日期:@DateTime.Now.ToString("yyyy-MM-dd");</span>

  5. }

2、Razor的作用域

         解释:在上面一个例子中都已经使用到了大括号{},不错,大括号里面的就是表示作用域的范围,用形如 “@{code}”来写一段代码块。在作用域 “@(code)” 中输出也是用@符号的。

         Index.cshtml页面:

 

 
  1. 我的年龄:

  2. @{

  3. int age = 25;

  4. string sex = "男";

  5. @age

  6. }性别:@(sex)

 

3、Razor和Html混合编写

         解释: 
         a.在作用域内容如果是以html标签开始则视为文本输出 
         b.如果输出@,则使用@@ 
         c.如果要输出非html标签和非Razor语句的代码,则用 "@:" ,他的作用是相当于在处于html下面编写一样了,如果在 "@:" 后面加上@就表示Razor语句的变量,如下:

        Index.cshtml页面:

 

 
  1. @{

  2. var strzm = "abc";

  3. @:this is a mail:2734796332@qq.com.this is var:@strzm,this is mail@strzm,this is @@

  4.  
  5. //输出abc

  6. @strzm

  7. }

 

4、Razor类型转换

         解释:As系列扩展方法和Is系列扩展方法(string类型才能转) 
           

 
  1. AsInt(),IsInt() 

  2.  AsBool(),IsBool() 

  3.  AsFloat(),IsFloat() 

  4.  AsDecimal(),IsDecimal() 

  5.  AsDateTime(),IsDateTime() 

  6. ToString()

        Index.cshtml页面:

 
  1. @{

  2. string ss = "123";

  3. }

  4. string转int:@ss.AsInt()

5、Razor其他

        解释:

       

 
  1. @Href("~/")//表示网站的根目录 

  2.  @Html.Raw('<font color='red'>红字</font>')就会显示出红色的”红字“,不用的话会直接显示这段html字符串(<font color='red'>红字</font>)

 

————————————————————————————————————————————

二、Razor引擎

1、布局(Layout)(@RenderBody()方法)

        解释:Layout方式布局就相当于一个模板一样的,我们在它地址地方去添加代码。相当于定义好框架,作为一个母版页的,在它下面的页面需要修改不同代码的地方使用@RenderBody()方法。 
        母版页:(~/Views/Layout/_SiteLayout.cshtml)

 
  1. <!DOCTYPE html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="utf-8"/>

  5. <title>我的网站 - @Page.Title</title>

  6. </head>

  7. <body>

  8. @RenderBody()

  9. </body>

  10. </html>

 

        子页面:(~/Views/Home/About.cshtml)

 
  1. @{

  2. Layout = "~/Views/Layout/_SiteLayout.cshtml";

  3. }

  4. <h1>

  5. 关于我的网站

  6. </h1>

  7. <p>

  8. 这是一些内容显示在关于我们这个页面,我们用的是SiteLayout.cshtml这个主页母版页。

  9. <br />

  10. 当前时间:@DateTime.Now

  11. </p>

 

 

 

2、页面(@RenderPage()方法)

 

 

        解释:page当需要在一个页面中,输出另外一个Razor文件(页面)的内容时候用到,比如头部或尾部这些公共的内容时需要用到,用@RenderPage()方法

        母版页:(~/Views/Layout/_SiteLayout.cshtml)

 

 
  1. <!DOCTYPE html>

  2.  
  3. <html>

  4. <head>

  5. <meta name="viewport" content="width=device-width" />

  6. <title>Simple Site</title>

  7. </head>

  8. <body>

  9. <!--头部-->

  10. @RenderPage("~/Views/Layout/_header.cshtml")

  11.  
  12. <!--底部-->

  13. @RenderPage("~/Views/Layout/_footer.cshtml")

  14. </body>

  15. </html>

 

        公共页:(~/Views/Layout/_header.cshtml)

 
  1. <div id="header">

  2. <a href="#">主页</a>

  3. <a href="#">关于我们</a>

  4. </div>

3、Section区域(@RenderSection())

 

 

      解释:Section是定义在Layout的页面中使用的。在Layout的页面中用。在要Layout的父页面中使用@RenderSection()方法。

       母版页:(~/Views/Layout/_SiteLayout.cshtml) 

 
  1. <!DOCTYPE html>

  2.  
  3. <html>

  4. <head>

  5. <meta name="viewport" content="width=device-width" />

  6. <title>Simple Site</title>

  7. </head>

  8. <body>

  9. <div id="left-menu">

  10. @RenderSection("menu",true)

  11. </div>

  12. </body>

  13. </html>

 

         公共页:(~/Views/Layout/_menu.cshtml)      

 

 
  1. @{

  2. Layout = "~/Views/Layout/_SiteLayout.cshtml";

  3. }

  4. <h1>

  5. 关于我的网站

  6. </h1>

  7. <p>

  8. 这是一些内容显示在关于我们这个页面,我们用的是SiteLayout.cshtml这个主页母版页。

  9. <br />

  10. 当前时间:@DateTime.Now

  11. </p>

  12. @section menu{

  13. <ul id="sub-menu">

  14. <li>菜单1</li>

  15. <li>菜单2</li>

  16. <li>菜单3</li>

  17. <li>菜单4</li>

  18. </ul>

  19. }

 

        如果在子页面中没有去实现了menu了,则会抛出异常。我们可以它的重载@RenderSection("menu", false)

 

 
  1. @if (IsSectionDefined("menu"))

  2. {

  3. @RenderSection("menu", false)

  4. }

  5. else {

  6. <p>menu Section is not defined!</p>

  7. }

4、Helper

 

     @helper就是可以定义可重复使用的帮助器方法,不仅可以在同一个页面不同地方使用,还可以在不同的页面使用。

     4.1、新建一个HelperMath.cshtml页面

image image

     4.2、HelperMath.cshtml页面写方法

 

 
  1. @*求和*@

  2. @helper sum(int a, int b)

  3. {

  4. int result = a + b;

  5. @result

  6. }

 

       4.3、Index.cshtml页面调用

1+2=@HelperMath.sum(1, 2)<br />

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439441.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)

题干&#xff1a; It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep…

让Team Foundation Server/TFS自动记住用户名密码解决方案

在使用Team Foundation Server&#xff08;以下简称TFS&#xff09; 的时候&#xff0c;在每次打开Visual Studio TFS时候&#xff0c;需要输入用户名和秘密&#xff0c;比较麻烦。 现提供一种方法可以解决这个问题&#xff1a; 依次执行下面操作&#xff1a; 打开控制面板--&…

【PAT - 甲级1017】Queueing at Bank (25分)(优先队列,模拟)

题干&#xff1a; Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be ser…

TFS(Team Foundation Server)敏捷使用教程

一、引言 1 中国式软件过程的坏味道 RUP&#xff0c;CMM/CMMI到了中国就变了味。。。。。。 2 Team Foundation Server TFS是软件开发的协作平台&#xff0c;它要解决的首要问题是团队成员的协作问题。比如说&#xff1a; 研发团队内部怎么协作&#xff0c;产品经理&#x…

【PAT - 甲级1020】Tree Traversals (25分)(树的遍历,给定中序后序,求层次遍历)

题干&#xff1a; Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. Input Specifi…

tfs 未能在以下位置创建报表文件夹 /TfsReports: 授予的权限不足,无法执行此操作

在tfs2015中添加新的Collection时&#xff0c;报一下错误&#xff1a; TF252015: 未能在以下位置创建报表文件夹: /TfsReports/XXXCollection。服务器返回了以下错误: 为用户“domain\name”授予的权限不足&#xff0c;无法执行此操作。。团队项目集合或项目将不具有对报表的访…

【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)

题干&#xff1a; A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Spe…

【PAT - 甲级1024】Palindromic Number (25分)(大数,模拟)

题干&#xff1a; A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic numbers can be pair…

银行卡密码的加密、MAC计算

简介 在银行、银联、第三方支付等金融系统中&#xff0c;对银行卡密码等信息的加解密&#xff0c;对交易数据的加解密无处不在&#xff0c;在商场刷卡消费的POS机&#xff0c;在ATM机器取款等都需要对数据加密以保护数据安全&#xff0c;不被窃取。 本文主要对POS机的安全处理…

【PAT甲级 - 1028】List Sorting (25分)(模拟,排序)

题干&#xff1a; Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤10​5​​) and C, where…

传统POS/终端/银联POS简介

point of sale&#xff1b; POS能够接受银行卡信息&#xff0c;具有通讯功能&#xff0c;并接受柜员的指令而完成金融交易信息和有关信息交换的设备。 找政策

【Python学习】 - - 链表推导式[ 2*x for x in X ]、匿名函数、并行迭代

列表推导式[x for x in range(n)] 问题&#xff1a;请计算出1~9间的整数的平方 常规方法 for i in range(1,10):print(i*i) 链表推导式&#xff1a; print([x*x for x in range(1,10)]) 匿名函数方法&#xff1a; 匿名函数语法形式&#xff1a; lambda [arg1, arg2, arg3,…

C#多线程和线程池

.Net的公用语言运行时&#xff08;Common Language Runtime&#xff0c;CLR&#xff09;能区分两种不同类型的线程&#xff1a;前台线程和后台线程。这两者的区别就是&#xff1a;应用程序必须运行完所有的前台线程才可以退出&#xff1b;而对于后台线程&#xff0c;应用程序则…

国家密码算法SM4(国密算法)介绍

国密是国家密码局认定的国产密码算法。而与之对应的&#xff0c;现在被广泛使用des、3des等算法是国外人发明&#xff0c;我们称为国际算法。 加密算法采用国家密码算法SM4&#xff0c;密钥长度为16字节&#xff0c;加密算法详见附录A。 主要有SM1&#xff0c;SM2&#xff0…

【最小费用可行流模板】

可能再也用不到了吧&#xff0c;今天整理电脑文件看到的&#xff0c;作为图论选手&#xff0c;留个纪念&#xff0c; //原图&#xff1a; 对于pi&#xff0c;拆点xi,yi s->S,[m,m],0 S->xi,[0,inf],0 yi->t,[0,inf],0 xi->yi,[vi,vi],0 对于有航线的pi和pj&#x…

ANSI X9.9 MAC算法介绍

1&#xff09;该算法只使用单倍长密钥&#xff0c;也就是8字节密钥&#xff1b; 2&#xff09;MAC数据按8字节分组&#xff0c;尾部以字节00补齐&#xff1b; 3&#xff09;用MAC密钥加密第一个8字节分组&#xff0c;加密结果与第二个8字节分组异或&#xff0c;然后再用MAC密…

C#.Net使用线程池(ThreadPool)与专用线程(Thread)

线程池(ThreadPool)使用起来很简单&#xff0c;但它有一些限制&#xff1a; 1. 线程池中所有线程都是后台线程&#xff0c;如果进程的所有前台线程都结束了&#xff0c;所有的后台线程就会停止。不能把入池的线程改为前台线 程。 2. 不能给入池的线程设置优先级或名称。 3. 对于…

【Python学习】 - sklearn学习 - 评估指标precision_score的参数说明

函数声明&#xff1a; precision_score(y_true, y_pred, labelsNone, pos_label1, averagebinary, sample_weightNone) 其中较为常用的参数解释如下&#xff1a; y_true&#xff1a;真实标签 y_pred&#xff1a;预测标签 average&#xff1a;评价值的平均值的计算方式。可…

ANSI X9.19 MAC算法介绍

(1) ANSI X9.19MAC算法只使用双倍长密钥&#xff0c;也就是16字节密钥&#xff1b; (2) MAC数据按8字节分组&#xff0c;表示为D0&#xff5e;Dn&#xff0c;如果Dn不足8字节时&#xff0c;尾部以字节00补齐&#xff1b; (3) 用MA…

GitHub.com上的那些东西你都知道什么意思吗?

GitHub初学入门者的图谱&#xff0c;介绍Github网站每个功能的意思 一、键盘快捷键 在GitHub中&#xff0c;很多页面都可以使用键盘快捷键。在各个页面按下“shift /”都可以打开键盘快捷键一览表&#xff0c;如下图&#xff1a; 快捷键 二、工具栏 工具栏 LOGO 点击GitHub…