博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RazorHelper.cs
阅读量:4610 次
发布时间:2019-06-09

本文共 6018 字,大约阅读时间需要 20 分钟。

 

完整版 RazorHelper.cs

 

using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Web;using RazorEngine;using RazorEngine.Text;namespace Console_Core.Common{    public class RazorHelper    {        ///         /// Razor解析cshtml页面,并输出到浏览器        ///         /// 上下文        /// cshtml页面的虚拟路径        /// 传递的虚拟实例        public static void RazorParse(HttpContext context, string cshtmlVirtualPath, object data)        {            string fullPath = context.Server.MapPath(cshtmlVirtualPath);            string cshtml = File.ReadAllText(fullPath);            string cacheName = fullPath + File.GetLastWriteTime(fullPath);            string html = Razor.Parse(cshtml, data, cacheName);            context.Response.Write(html);        }        ///         /// 对html进行加密        ///         /// html标签        /// 
加密之后的字符串
public static HtmlEncodedString HtmlEncodedString(string htmlStr) { return new HtmlEncodedString(htmlStr); } /// /// 对html原样显示 /// /// html标签 ///
html原来样子
public static RawString RawString(string htmlStr) { return new RawString(htmlStr); } /// /// 拼接生成CheckBox 标签 /// /// 是否选中 /// 扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' } ///
CheckBox标签
public static RawString CheckBox(bool isCheck, object extendProperties) { StringBuilder sb = new StringBuilder(); sb.Append(""); return new RawString(sb.ToString()); } /// /// 拼接扩展属性 及对应的值 /// /// 扩展属性 所在的匿名实例 ///
拼接生成的 包含属性名和值 的字符串: 比如,“ name='manager' id='managerId' ”
private static string RenderExtProperties(object extendProperties) { StringBuilder sb = new StringBuilder(); #region 拼接扩展属性 Type extType = extendProperties.GetType(); PropertyInfo[] props = extType.GetProperties(); foreach (PropertyInfo prop in props) { string extPropName = prop.Name; object extPropValue = prop.GetValue(extendProperties); sb.Append(" ").Append(extPropName).Append("='").Append(extPropValue).Append("' "); } #endregion return sb.ToString(); } /// /// 拼接生成DropDownList下拉列表 标签 /// /// 实例的集合 /// 实际的值属性的名称:比如,Id /// 显示的文本属性的名称:比如,Name /// 选中的值 /// 扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' } ///
DropDownList下拉列表 标签
public static RawString DropDownList(IEnumerable list,string valuePropName,string textPropName,object selectedValue,object extendProperties) { // StringBuilder sb = new StringBuilder(); sb.Append(""); return new RawString(sb.ToString()); } /// /// 拼接生成RadioButtonList 标签 /// /// 实例的集合 /// 实际的值属性的名称:比如,Id /// 显示的文本属性的名称:比如,Name /// 选中的值 // 扩展属性的对象:比如,new {name='gender',style='color:red' } ///
RadioButtonList 标签
public static RawString RadioButtonList(IEnumerable list, string valuePropName, string textPropName, object selectedValue, object extendProperties) { //
//只能单选 StringBuilder sb = new StringBuilder(); foreach(object item in list) { object valuePropValue, textPropValue; GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue); sb.Append("
"); } return new RawString(sb.ToString()); } /// /// 拼接生成CheckBoxList 标签 /// /// 实例的集合 /// 实际的值属性的名称:比如,Id /// 显示的文本属性的名称:比如,Name /// 选中的值的数组 /// 扩展属性的对象:比如,new {name='hobby',style='color:red' } ///
CheckBoxList 标签
public static RawString CheckBoxList(IEnumerable list, string valuePropName, string textPropName, object[] selectedValues, object extendProperties) { //
//可多选 StringBuilder sb = new StringBuilder(); foreach(object item in list) { object valuePropValue,textPropValue; GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue); sb.Append("
"); } return new RawString(sb.ToString()); } /// /// 根据指定实例的 值属性名和文本属性名 获得 值属性值和文本属性值 /// /// 指定实例 /// 值属性名 /// 文本属性名 /// out 值属性值 /// out 文本属性值 private static void GetvalueAndTextPropValue(object item, string valuePropName, string textPropName, out object valuePropValue, out object textPropValue) { Type type = item.GetType(); PropertyInfo valueProp = type.GetProperty(valuePropName); valuePropValue = valueProp.GetValue(item); PropertyInfo textProp = type.GetProperty(textPropName); textPropValue = textProp.GetValue(item); } }}
RazorHelper.cs

 

posted on
2015-09-15 20:32 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/adolphyang/p/4811400.html

你可能感兴趣的文章
linux vi编辑器
查看>>
js树形结构-----(BST)二叉树增删查
查看>>
contract
查看>>
FJUT ACM 1899 Largest Rectangle in a Histogram
查看>>
如何删除xcode项目中不再使用的图片资源
查看>>
编写用例文档
查看>>
解决WPF两个图片控件显示相同图片因线程占用,其中一个显示不全的问题
查看>>
作为一个c#偏爱前端的程序员2017年我都该做点什么
查看>>
寻觅Azure上的Athena和BigQuery (二):神奇的PolyBase
查看>>
Android Gradle基础实践
查看>>
ITFriend站点内測公測感悟
查看>>
[BZOJ2763][JLOI2011]飞行路线
查看>>
ajax提交表单,支持文件上传
查看>>
java获取指定文件夹下的所有文件名
查看>>
Spring学习十一
查看>>
LayIM.AspNetCore Middleware 开发日记(六)嵌入资源的使用,layim.config的封装
查看>>
CMD网络连接命令
查看>>
2017 计蒜之道 初赛 第六场 !
查看>>
js函数
查看>>
二叉排序树转化平衡二叉排序树(转载)
查看>>