2014-09-18 14:06 by 易code, 2854 阅读, 0 评论, 收藏, 编辑

1 Code Expressions  代码表达式

@表达式  or  @(Expressions )

例如1: <h1>Listing @stuff.Length items.</h1

Razor peeks at the next character and sees an angle bracket, which isn’t a valid identifier and transitions back into markup mode

例如2:

@{
string rootNamespace = "MyApp";
}
<span>@rootNamespace.Models</span>

What hope to output was  :

<span>MyApp.Models</span>

必须这样写:<span>@(rootNamespace).Models</span> ,通过括号来标记语法结束。

example 3:

How to show an e-mail address:

<span>support@megacorp.com</span>                      Razor 语法能智能识别 一个 正确的e-mail 地址

但是像微博这种,@名字    ,则需要转义输出:

<p>
You should follow
@haacked, @jongalloway, @bradwilson, @odetocode
</p>

You can do so by using @@ sing ,just like that:

<p>
You should follow
@@haacked, @@jongalloway, @@bradwilson, @@odetocode
</p>

2 HTML Encoding

为了跨站点的脚本攻击,Razor 语法会直接将脚本代码编码输出。

@{
string message = "<script>alert('haacked!');</script>";
}
<span>@message</span>

输出:

<span>&lt;script&gt;alert('haacked!');&lt;/script&gt;</span>

此条实际测试时(chrome版本 37.0.2062.120 m,IE11)均显示为: <span><script>alert('haacked!');</script></span>

如果需要原样输出 代码文本 需要使用 @Html.Raw():

@{
string message = "<strong>This is bold!</strong>";
}
<span>@Html.Raw(message)</span>
输出:
<span><strong>This is bold!</strong></span>

实际测试: 显示为:This is bold!     当作html标记执行了!!!

如果 message ="<script>alert('haacked!');</script>"; 那么

<span>@Html.Raw(message)</span>  会弹出一个 haacked!  对话框,执行js脚本!

可以在 Jquery 语法中使用 Razor语法:

<script type="text/javascript">
        $(function () {
            var message = 'Hello @ViewBag.Title';
    $("#message").html(message).show('slow');
});
</script>

结果: Hello My Sample title

可以使用 @Ajax.JavaScriptStringEncode(string value)   对 字符内容进行编码

3  Code Block  代码块

@{ }                           代码块

例如:

@{
string s = "One line of code.";
ViewBag.Title "Another line of code";
}

@foreach(){}   , @while() {} ;@for(){}             循环代码块

@if(){} ;  条件块

@*      *@     注释块

例如:

@foreach (Mvc4App.Models.LoginModel login in ViewBag.LoginModels)
          {
               UserName:<li>@login.UserName</li>
          }

4 Layouts

用来呈现布局设计。@RenderBody()

例如: 主布局:SiteLayout.cshtml 文件

<!DOCTYPE html>
<html>
<head><title>@ViewBag.Title</title></head>
<body>
The Razor View Engine x 67
<h1>@ViewBag.Title</h1>
<div id="main-content">@RenderBody()</div>
</body>
</html>

使用布局文件 index.cshtml

@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
View.Title = "The Index!";
}
<p>This is the main content!</p>

---------------------------------------------

呈现 布局段   @RenderSection()

<!DOCTYPE html>
<html>
<head><title>@ViewBag.Title</title></head>
<body>
<h1>@ViewBag.Title</h1>
<div id="main-content">@RenderBody()</div>
<footer>@RenderSection("Footer")</footer>
</body>
</html>

更新index.cshtml

@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
View.Title = "The Index!";
}
<p>This is the main content!</p>
@section Footer {
This is the <strong>footer</strong>.
}

-------------------------------------------------------

如果有些需要呈现,有些不需要,可以使用RenderSection 的重载方法:

<footer>@RenderSection("Footer", required: false)</footer>

还可以判断段是否存在

<footer>
@if (IsSectionDefined("Footer")) {
RenderSection("Footer");
}
else {
<span>This is the default footer.</span>
}
</footer>

------------------------------------------------------------

5  分布视图 partial view

在HomeControl 中添加Action

public ActionResult Partial()
      {
          ViewBag.Part = "this is partial view";
          return PartialView();
      }

右键添加视图partial.cshtml,分布视图不能使用Layout 属性。

<div>
    <p>Hello @ViewBag.Part</p>
</div>

分布视图与一般视图可以一样的方式浏览  http://localhost:46918/home/partial

分布视图可以同Ajax 配合,直接加载:

<input type="button" value="click me to load partial view" onclick="loadPartil()" />
    <div id="result"></div>
<script type="text/javascript">
    function loadPartil() {
        $('#result').load('/home/partial');
    }
</script>

ASP.NET MVC4 Razor的更多相关文章

  1. ASP.NET MVC4(Razor)从客户端中检测到有潜在危险的 Request.Form 值

    SP.NET MVC4(Razor)从客户端中检测到有潜在危险的 Request.Form 值 “/”应用程序中的服务器错误. 从客户端(Content=" sdfdddd ..." ...

  2. 21、ASP.NET MVC入门到精通——ASP.NET MVC4优化

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 删除无用的视图引擎 默认情况下,ASP.NET MVCE同时支持WebForm和Razor引擎,而我们通常在同一个项目中只用到了一种视图引擎 ...

  3. Asp.Net MVC4 + Oracle + EasyUI 学习 第二章

    Asp.Net MVC4 + Oracle + EasyUI 第二章 --使用Ajax提升网站性能 本文链接:http://www.cnblogs.com/likeli/p/4236723.html ...

  4. Asp.Net MVC4 + Oracle + EasyUI 学习 第一章

    Asp.Net MVC4 + Oracle + EasyUI  第一章 --操作数据和验证 本文链接:http://www.cnblogs.com/likeli/p/4234238.html 文章集合 ...

  5. Asp.Net MVC4 + Oracle + EasyUI 学习 序章

    Asp.Net MVC4 + Oracle + EasyUI  序章 -- 新建微软实例 本文链接:http://www.cnblogs.com/likeli/p/4233387.html 1.  简 ...

  6. asp.net mvc4 学习笔记一(基本原理)

    做了8年的asp.net webform,用过MVVM但还没用过MVC , 虽然项目不用MVC,但是还是想了解一下,今天第二天学习,以下是学习心得. VS2012默认带有asp.net mvc3和as ...

  7. 利用CSS预处理技术实现项目换肤功能(less css + asp.net mvc4.0 bundle)

    一.背景 在越来越重视用户体验的今天,换肤功能也慢慢被重视起来.一个web系统用户可以选择一个自己喜欢的系统主题,在用户眼里还是会多少加点分的.我们很开心的是easyui v1.3.4有自带defau ...

  8. Asp.Net MVC4入门指南(1): 入门介绍

    前言 本教程将为您讲解使用微软的Visual Studio Express 2012或Visual Web Developer 2010 Express Service Pack 1 来建立一个ASP ...

  9. ASP.NET MVC4 部分视图

    ASP.NET MVC4 部分视图 2014-10-24 16:48 by 易code, 2662 阅读, 1 评论, 收藏, 编辑 [部分视图] ASP.NET MVC 里的部分视图,相当于 Web ...

随机推荐

  1. C++ vector 删除一个指定元素 和 find 一个指定元素以及遍历删除、 map遍历删除元素和删除find到的元素

    vector: 1.delete element 转载:http://www.cnblogs.com/xudong-bupt/p/3522457.html #include <vector> ...

  2. Tomcat下使用Log4j,按日期每天存放,解决catalina.out日志文件过大问题

    1. 准备jar包: log4j-1.2.17.jar (从 http://www.apache.org/dist/logging/log4j/1.2.17/ 下载) tomcat-juli.jar, ...

  3. P3066 [USACO12DEC]逃跑的BarnRunning Away From

    目录 题目 思路 错误&&注意 代码 题目 luoguP3066 思路 虽说这个题目有多种做法,但 左偏树算法: 我们发现这个合并的时候并不好合并,因为存的值不是固定的 那我们是不是可 ...

  4. P3952 NOIP2017 时间复杂度

    写了两三个小时,麻烦倒是不麻烦,要考虑清楚,想全了 只过了样例提交是不是傻,要自己造数据 数据不大可以用STL 建议自己刚一下,不看代码 #include <iostream> #incl ...

  5. 奇怪的分式|2014年蓝桥杯B组题解析第六题-fishers

    奇怪的分式 上小学的时候,小明经常自己发明新算法.一次,老师出的题目是: 1/4 乘以 8/5 小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png) 老师刚想批评他,转念 ...

  6. java 正则表达式验证邮箱格式是否合规 以及 正则表达式元字符

    package com.ykmimi.testtest; /** * 测试邮箱地址是否合规 * @author ukyor * */ public class EmailTest { public s ...

  7. C#学习笔记(十四):多态、虚方法和抽象类

    虚方法/非虚方法 < 实例方法 = 非静态方法 = 非类方法(非实例方法 = 静态方法 = 类方法) 函数签名(参数列表,或参数列表 + 返回类型) using System; using Sy ...

  8. FAST UA API

    参考: FAST_UA 编程手册 FAST DATA STRUCTURE fast_packet fast_metadata fast_rule fast_flow FAST UA API 1.fas ...

  9. 回顾:C++开篇课堂

    周三,在刚刚结束矩阵分析的考试之后,面向对象程序设计课正式开始了C++的学习,这次课的重点即是面向对象思想的理解. 梳理一下知识点. C++中函数的默认参数 在函数声明或者在函数定义时,对参数赋初始值 ...

  10. bind9安装配置

    BIND的安装配置: dns服务,程序包名叫bind,程序名named 程序包: bind bind-libs bind-utils bind-chroot: /var/named/chroot/ b ...