Razor 的一些简单的语法

Razor是基于framewor4以上写的一个开源项目:

Razor的发布是和MVC一起的,作为MVC的视图模板引擎。可以嵌套在html中一起使用

Razor文件类型 :Razor可以在vb.net和C#中使用。分别对应了两种文件类型,.vbhtml和.cshtml

定义使用变量 ,输出 一些html元素

        @*定义一个变量*@
        @{
            string userName = "李四";
            int i = 1;
            DateTime today = DateTime.Today;
        }
        <span>@userName @i @today</span>
        <span>@DateTime.Now.ToString("yyyy-MM-hh")</span>
        <br />
        @*Razor的作用域
            用形如@{code}来写一段代码块, 大括号里面的就是表示作用域的范围,*@
        @{
            var str = "张三";
            @: this is Name @str @*输出语句*@
        }
        你好 @str

        @*输出html元素*@
        @Html.Raw("<font color='red'>红字</font>");ddd
        @Html.TextBox("id ='id'")
View Code

判断语句: if else 语句

        @{var price = 0;}
        @if (price > 5)
        {
            <p>The price is too high.</p>
        }
        else
        {
            <p>The price is OK.</p>
        }

循环语句

 @*for循环*@
        @for ( int i = 10; i < 21; i++)
            {<p>Line @i</p>}
        
        @*while 循环 *@
        @{
            var d = 0;
            while (d < 5)
            {
                d += 1;
                <p>Line @d</p>
            }
        }

当然循环还有foreach ;

这里就不多说了。查考资料: Razor

posted @ 2017-02-18 10:57  游林  阅读(133)  评论(0)    收藏  举报