Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace _04_RazorExpression.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
}
View:
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<div>
<h1>
1.基本字符串数组操作</h1>
@{var items = new string[] { "one", "two", "three" };}
<ul>
@{foreach (string item in items)
{
<li>The item name is : @item</li>
}
}
</ul>
Items total:@items.Length;
<h1>
2.显式代码(括号)表达式:@@()</h1>
@{string myApp = "Model";}
<span>@(myApp).Models</span>
<h1>
3.@@转义符号:@@@@</h1>
<span>zhangqs008@@163.com</span>
<h1>
4.Html编码输出:Html.Raw()</h1>
@{string message = "<script type='text/javascript'>alert('Hello!');</script>";
string message2 = "<b>alert('Hello!');</b>";
}
<span>@message</span> <span>@Html.Raw(@message2)</span>
<h1>
5.javascript中的编码:Ajax.JavaScriptStringEncode()</h1>
<script type="text/javascript">
$(function () {
var message = "Hello @ViewBag.Message";
$("#output").html(message).show();
//当在Javascript中将用户提供的值赋给变量时,要使用javascript字符串编码,而不仅仅是Html编码,记住这一点很重要。
var message2 = "Hello @Ajax.JavaScriptStringEncode(@ViewBag.Message)";
$("#output2").html(message2).show();
});</script>
<span id="output"></span>
<br />
<span id="output2"></span>
<h1>
6.混合代码和纯文本:@@:</h1>
@{ bool showMessage = true;
if (showMessage)
{
@:this is plain text
}
}
<h1>
7.代码注释 @@*...*@@</h1>
@*{
bool showMessage = true;
if (showMessage)
{
@:this is plain text
}}
*@
</div>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
</p>
效果图:
分享到:
相关推荐
Razor原版文档,讲的挺好,英文版,有助于理解.Net 中 MVC Razor引擎
安装 通过安装,用于ASP.NET Core: ...与Razor表达式兼容 方法操作内部模型,而不是字符串。 在调用ToString()之前,不会生成HTML字符串。 最后一点似乎微不足道,但这实际上是HtmlTag的最大优势。 如果您的Htm
这个是我本人写的《在winform使用razor模板引擎》的一个例子,参考了msdn上面的文章(作者:Matt Wrock,文章名称:Using the Razor templating engine outside of MVC)及某篇在winform上面配置razor 智能提示的文章...
Razor connector
Razor视图引擎语法 注意事项 等等
Razor出现后我们就可以选择不再使用asp.net master 模板页。取而代之的是cshtml razor的模板文件。
razor开发必备的语法,有简单的实例应用,对于刚接触razor的朋友很有帮助。
一个 Razor模板解析的例子,包括了解析Razor模板,传入dynamic 类型的Model,以及模板的执行
Programming Razor 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
这是本人翻译的一篇英文文章【Hosting the Razor Engine for Templating in Non-Web Applications】里面的例子,可以直接使用,有兴趣可以看我的译文版【假如你不嫌弃翻译渣的话】,也可以直接查看原文地址:...
增删改查 登录功能 注册功能 新手上路必备 此项目有后台
Razor的语法,Razor的语法,Razor的语法
Golang的Razor视图引擎
WebMatrix中的Razor(源码) WebMatrix中的Razor(源码)
Razor语法的示例程序包括页面生成 数据库使用 邮件使用 一用俱全
jqGrid in ASP.NET MVC 3 and Razor.zipjqGrid in ASP.NET MVC 3 and Razor.zipjqGrid in ASP.NET MVC 3 and Razor.zip
MVC3,aspx2razor可以方便将ASPX转换成RAZOR视图的文件,便于MVC3开发.
网上找的把razor引擎独立使用的一个项目
ASP.NET Core 2.0 MVC Razor Pages
在非MVC环境下使用 Razor引擎,方便使用思路不错