惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 互联网粒子

论公司运营 复杂问题需要系统思维 项目管理-项目进度偏差分析 无线互联的三大机会 IT公司的情.理.法.文管理 康盛被腾讯“招安” - 互联网粒子 - 博客园 在WIN7系统IIS7下配置PHP5运行环境 基于JSON缓存的多国语言的实现 IIS7中启用JS的压缩 用户个性化推荐 IE6下的JQUERY_FCK兼容问题 - 互联网粒子 - 博客园 软件需求全景 .NET中使用WINDOWS API参数定义 如何下载安装和破解VS2010 windows下面的管理命令 数据库字符串内容批量更新 如何查询Sql Server 2005补丁版本号 javascript模板机制 javascript上实现动态参数
Razor视图引擎-基础语法
互联网粒子 · 2011-10-09 · via 博客园 - 互联网粒子

-基础-

所有以 @开头 或 @{ /* 代码体 */ }  (在@与{直接不得添加任何空格) 的部分代码都会被ASP.NET引擎进行处理.
在 @{ /*代码体*/ } 内的代码每一行都必须以";"结束,如
@{
    var i = 10;
    var y = 20;
}
而 @xxx 则不需要以";"作为结束符,如
@i 输出 10
@y; 输出 20;
代码区内字母分大小写.
字符类型常量必须用""括起例如: @{ string str = "my string"; }
-注意-

如需要在页面输出”@”字符
可以使用HTML ASCII编码@
当然Razor也提供智能分析功能: 如果在@的前一个字符若是非空白字符,则ASP.NET不会对其进行处理
如:<p>text@i xx</p> 输出 text@i xx

单行语法:
@{ var I = 10; }
多行语法:

@{ 
    var I = 10;
    Var y = 20;
}

1. 使用局部变量,Razor不支持访问修饰符(public,private等,这个没任何意义)
在单行上定义局部变量
@{ var total = 7; }
@{ var myMessage = "Hello World";}
在多行上定义局部变量

@{
    var greeting = "Welcome to our site!";
    var weekDay = DateTime.Now.DayOfWeek;
    var greetingMessage = greeting + " Today is: " + weekDay;
}


在上下文中使用变量

<p>The value of your account is: @total </p>
<p>The value of myMessage is: @myMessage</p>


注意
:变量拼接输出
@{ var i = 10; }
<p>text @i text</p> 将输出 text 10 text
但是如果你想要输出 text10text 呢?

<p>text@{@i}text</p>即可
<p>text@i text</p> 将输出 text@i text
<p>text@itext</p> 将输出 text@itext
<p>text @itext</p> 将报错

如果是输出的是变量的方法名则不需要用@{}括住也可生效,但注意在@字符前记得加空格(感谢spook指出)如:
<p>text @i.ToString()text</p>
使用变量对象可直接写: @var1 @var2 @myObject.xx

2. 使用逻辑处理

@{
    if (xx)
    {
    //do something
    }
    else
    {
    //do anything
    }
}

3. 在@{... }内部使用html标记

@{
    <p>text</P>
    <div>div1</div>
}

4. 在@{...}内部输出文本
利用@:进行单行输出:

@{
    @:This is some text
    @:This is text too
    @:@i 也可输出变量
}

利用<text />进行多行输出:

@{
    <text>
        tomorrow is good
        some girl is nice
    </text>
}
5. 在@{...}内部使用注释
@{

//单行注释

var i = 10;

//defg

}

@* 多行注释 *@

@*

多行注释

多行注释

*@

@{

@*

多行注释

多行注释

*@

var i = 10; @* asdfasf *@

}

<!-- 同时也可以使用C#默认的/* ... */ -->

@{

/*

多行注释

*/

}

若在@{ ... }内部使用<!-- -->注释,则会输出到页面之中,如果在<!-- -->内部使用@变量,则会被处理
@{
<!-- time now: @DateTime.Now.ToString() -->
}
输出: <!-- time now: 4/9/2011 12:01 -->>

6. 类型转换
AsInt(), IsInt()
AsBool(),IsBool()
AsFloat(),IsFloat()
AsDecimal(),IsDecimal()
AsDateTime(),IsDateTime()
ToString()
例子:

@{
    var i = “10”;
}
 
<p> i = @i.AsInt() </p> <!-- 输出 i = 10 --> 

7. 使用循环

<!--方式1-->

@for (int i = 10; i < 11; i++)

{

    @:@i

}
<!--方式2-->

@{

    for (int i = 10; i < 11; i++)

    {

        //do something

    }

}

<!--while同理-->