mvc4 自定义辅助器及辅助器中的字符串编码
来源:互联网 发布:职人介绍所 知乎 编辑:程序博客网 时间:2024/04/27 21:27
一、创建自定义辅助器
1、创建内联的辅助器方法
创建“基本”的mvc示例,并创建Home控制器,及index视图
Home:
public ActionResult Index() { ViewBag.aa = new string[] { "aa", "aaa", "aaaa" }; ViewBag.bb = new string[] { "bb", "bbb", "bbbb" }; string message = "this is an HTML element:<input>"; return View((object)message); }
index.cshtml
@{ Layout = null;}<!DOCTYPE html>@model string<html><head> <meta name="viewport" content="width=device-width" /> <title>Index</title></head><body> @*创建自定义的辅助器*@ @helper ListArrayItems(string[] item) { <ul> @foreach (string s in item) { <li>@s</li> } </ul>} <div> this is a:@ListArrayItems(ViewBag.aa) </div> <div> this is b:@ListArrayItems(ViewBag.bb) </div> <div> this is messige:@Model </div></body></html>
内联辅助器具有方法名和参数,这个例子定义了ListArrayItems的辅助器,它是没有返回值,结果直接输出到客户端的响应中,只需要修改ListArrayItems里的内容即可。
2、创建外部的辅助器方法
新建一个静态类如下:
public static class CustomHelper { public static MvcHtmlString ListArrayItemsOther(this HtmlHelper html,string[] list) { TagBuilder tag = new TagBuilder("ul"); foreach(string s in list) { TagBuilder ItemTag = new TagBuilder("li"); ItemTag.SetInnerText(s);//对s进行编码 tag.InnerHtml += ItemTag.ToString(); } return new MvcHtmlString(tag.ToString()); } }
返回了 MvcHtmlString 类型,this HtmlHelper html 表示这是一个扩展方法,TagBulider创建html元素,不用使用大量转意符、尖括号等,非常方便。
调用外部辅助器首先要引用命名空间,然后调用方法如下:
<div> 外部辅助器:@Html.ListArrayItemsOther((string[])ViewBag.aa) </div>
mvc框架通过对数据进行自动编码实现,以便能够将数据安全的添加到web页面,上述例子,在Home控制器的index方法里,输出了一段文本,如下:
<span style="white-space:pre"></span> string message = "this is an HTML element:<input>"; return View((object)message);当Razor渲染该值时结果如下:
这是一种基本的安全保护,它可以防止数据值解释成为一个有效的标签,Razor会自动对视图中使用的数据进行编码,但是辅助器的方法却能够生成HTML,下面是详细演示:在刚刚建立的静态类CustomHelper中添加一个静态方法如下:
<span style="white-space:pre"></span>public static MvcHtmlString DisplayMessage(this HtmlHelper html,string str) { string result = String.Format("This is the message:<p>{0}</p>", str); return new MvcHtmlString(result); }视图调用方法如下:
<div> this is messige:@Model </div> <div> @Html.DisplayMessage(Model) </div>输出结果如下:
可以看出使用辅助器渲染出的结果是直接生成了html标签。
那么如何对辅助器内容进行编码呢,方法有两种:
第一种:
public static string DisplayMessage(this HtmlHelper html,string str) { string result = String.Format("This is the message:<p>{0}</p>", str); return result; }生成的结果如下:
可以看出如果返回的是string,mvc框架自动对内容进行编码,但是有个问题<p>标签也被编码显示成文本,这个时候要利用另外一种方法,
第二种:
public static string DisplayMessage(this HtmlHelper html,string str) { string tempStr = html.Encode(str); string result = String.Format("This is the message:<p>{0}</p>", tempStr); return result; }输出结果如下:
由结果看出 这只是把用户的参数进行编码,而<p>标签被解析成html标签,这就是我们想要的结果。
1 0
- mvc4 自定义辅助器及辅助器中的字符串编码
- 自定义辅助
- QQ找茬辅助器
- 数独辅助器
- 自定义辅助视图accessoryview
- StringHelper-字符串辅助类
- 简单实现图库辅助器
- 会计中的辅助核算
- tableview自定义内容视图contentView及辅助视图accessoryType
- 自定义控件辅助神器ViewDragHelper
- 自定义控件辅助神器ViewDragHelper
- Java 多线程 自定义线程辅助
- Squid第12章 验证辅助器
- php写的squid验证辅助器
- QQ美女找茬辅助器制作
- QQ美女找茬辅助器源代码~~~
- 编程辅助软件之代码查看器
- 动画差值器的计算辅助网站
- ORA-28040 no matching authentication protocol
- JS提示框
- 《架构之美》读后感
- UCOS-ii在ARM处理器上的移植
- Android的webview缩放设置
- mvc4 自定义辅助器及辅助器中的字符串编码
- JS中string方法中常用方法之十一:String.prototype.toLocaleUpperCase()
- bzoj 1898: [Zjoi2005]Swamp 沼泽鳄鱼
- Git fetch和git pull的区别
- ubuntu美化--修改图标
- M102: MongoDB for DBAs - Final Exam
- leetcode--best_time_to_buy_and_sell_stock
- CSS基础复习之定位
- 孩子们的游戏(圆圈中最后剩下的数)