完整版 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
{
/// <summary>
/// Razor解析cshtml页面,并输出到浏览器
/// </summary>
/// <param name="context">上下文</param>
/// <param name="cshtmlVirtualPath">cshtml页面的虚拟路径</param>
/// <param name="data">传递的虚拟实例</param>
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);
} /// <summary>
/// 对html进行加密
/// </summary>
/// <param name="htmlStr">html标签</param>
/// <returns>加密之后的字符串</returns>
public static HtmlEncodedString HtmlEncodedString(string htmlStr)
{
return new HtmlEncodedString(htmlStr);
} /// <summary>
/// 对html原样显示
/// </summary>
/// <param name="htmlStr">html标签</param>
/// <returns>html原来样子</returns>
public static RawString RawString(string htmlStr)
{
return new RawString(htmlStr);
} /// <summary>
/// 拼接生成CheckBox 标签
/// </summary>
/// <param name="isCheck">是否选中</param>
/// <param name="extendProperties">扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' }</param>
/// <returns>CheckBox标签</returns>
public static RawString CheckBox(bool isCheck, object extendProperties)
{
StringBuilder sb = new StringBuilder();
sb.Append("<input type='checkbox' ");
sb.Append(RenderExtProperties(extendProperties));
if(isCheck)
{
sb.Append(" checked ");
}
sb.AppendLine(" />");
return new RawString(sb.ToString());
} /// <summary>
/// 拼接扩展属性 及对应的值
/// </summary>
/// <param name="extendProperties">扩展属性 所在的匿名实例</param>
/// <returns>拼接生成的 包含属性名和值 的字符串: 比如,“ name='manager' id='managerId' ” </returns>
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();
} /// <summary>
/// 拼接生成DropDownList下拉列表 标签
/// </summary>
/// <param name="list">实例的集合</param>
/// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
/// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
/// <param name="selectedValue">选中的值</param>
/// <param name="extendProperties">扩展属性的对象:比如,new {id='managerId',name='manager',style='color:red' }</param>
/// <returns>DropDownList下拉列表 标签</returns>
public static RawString DropDownList(IEnumerable list,string valuePropName,string textPropName,object selectedValue,object extendProperties)
{
//<select name='' id='' >
//<option value=''> </option>
//</select>
StringBuilder sb = new StringBuilder();
sb.Append("<select ");
#region 拼接扩展属性
sb.Append(RenderExtProperties(extendProperties));
#endregion
sb.AppendLine(" >");
#region 拼接下拉选项
foreach (object item in list)
{
object valuePropValue, textPropValue;
GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
sb.Append("<option value='").Append(valuePropValue).Append("' ");
if(object.Equals(valuePropValue,selectedValue)) //如果当前值与选中的值相等,则selected (引用类型用equal,如果用=则是不同的实例,因为发生过装箱)
{
sb.Append(" selected ");
}
sb.Append(">").Append(textPropValue).AppendLine(" </option> ");
}
#endregion
sb.AppendLine("</select>");
return new RawString(sb.ToString());
} /// <summary>
/// 拼接生成RadioButtonList 标签
/// </summary>
/// <param name="list">实例的集合</param>
/// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
/// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
/// <param name="selectedValue">选中的值</param>
// <param name="extendProperties">扩展属性的对象:比如,new {name='gender',style='color:red' }</param>
/// <returns>RadioButtonList 标签</returns>
public static RawString RadioButtonList(IEnumerable list, string valuePropName, string textPropName, object selectedValue, object extendProperties)
{
//<input type="radio" name="gender" value="1" checked /><label>男</label><br /> //只能单选
StringBuilder sb = new StringBuilder();
foreach(object item in list)
{
object valuePropValue, textPropValue;
GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
sb.Append("<input type=\"radio\" ");
sb.Append(RenderExtProperties(extendProperties));
sb.Append(" value=\"").Append(valuePropValue).Append("\"");
if(object.Equals(valuePropValue,selectedValue))
{
sb.Append(" checked ");
}
sb.Append(" /><label>").Append(textPropValue).AppendLine("</label><br />");
}
return new RawString(sb.ToString());
} /// <summary>
/// 拼接生成CheckBoxList 标签
/// </summary>
/// <param name="list">实例的集合</param>
/// <param name="valuePropName">实际的值属性的名称:比如,Id</param>
/// <param name="textPropName">显示的文本属性的名称:比如,Name</param>
/// <param name="selectedValues">选中的值的数组</param>
/// <param name="extendProperties">扩展属性的对象:比如,new {name='hobby',style='color:red' }</param>
/// <returns>CheckBoxList 标签</returns>
public static RawString CheckBoxList(IEnumerable list, string valuePropName, string textPropName, object[] selectedValues, object extendProperties)
{
//<input type="checkbox" name="hobby" value="1" checked /><label>足球</label><br /> //可多选
StringBuilder sb = new StringBuilder();
foreach(object item in list)
{
object valuePropValue,textPropValue;
GetvalueAndTextPropValue(item, valuePropName, textPropName, out valuePropValue, out textPropValue);
sb.Append("<input type=\"checkbox\" ");
sb.Append(RenderExtProperties(extendProperties));
sb.Append (" value=\"").Append(valuePropValue).Append("\" ");
if(selectedValues.Contains(valuePropValue))
{
sb.Append(" checked ");
}
sb.Append(" /><label>").Append(textPropValue).AppendLine("</label><br />");
}
return new RawString(sb.ToString());
} /// <summary>
/// 根据指定实例的 值属性名和文本属性名 获得 值属性值和文本属性值
/// </summary>
/// <param name="item">指定实例</param>
/// <param name="valuePropName">值属性名</param>
/// <param name="textPropName">文本属性名</param>
/// <param name="valuePropValue">out 值属性值</param>
/// <param name="textPropValue">out 文本属性值</param>
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

RazorHelper.cs的更多相关文章

  1. RazorEngine在非MVC下的使用,以及使用自定义模板

    ---恢复内容开始--- RazorEngine模板引擎大大的帮助了我们简化字符串的拼接与方法的调用,开源之后,现在在简单的web程序,winform程序,甚至控制台程序都可以利用它来完成. 但如何在 ...

  2. DIDAO.Common --- 项目中的常用类及其中函数

    常用函数: CommonHelper.cs using System; using System.Collections.Generic; using System.IO; using System. ...

  3. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  4. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结

    Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...

  5. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  6. 关于 WP 开发中.xaml 与.xaml.cs 的关系

    今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...

  7. .net 用户控件ascx.cs注册js脚本代码无效果

    在.net web项目中碰到一个比较奇怪的问题,网上没找到解决方案,先自己mark一下 问题描述: 添加一个用户控件ascx,在后端.cs添加js注册脚本,执行后没有弹出框 注册脚本为: this.P ...

  8. DateHelper.cs日期时间操作辅助类C#

    //==================================================================== //** Copyright © classbao.com ...

  9. 仅用aspx文件实现Ajax调用后台cs程序。(实例)

    仅用aspx文件实现Ajax调用后台cs无刷新程序.(实例) 两个文件:aaa.aspx 和aaa.aspx.cs 一.aaa.aspx <script type="text/java ...

随机推荐

  1. 父元素设置overflow,绝对定位的子元素会被隐藏或一起滚动

    一般情况: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <met ...

  2. 大话设计模式之PHP篇 - 简单工厂模式

    假设有一道编程题:输入两个数字和运算符,然后得到运算结果.非常简单的一道题目,通常的实现代码如下: <?php Function Operation($val1, $val2, $operate ...

  3. 1.python基本数据类型

    1.数值类型(4种) 数值类型都是不可变的 1)整型(int) example: a = 0 b = -1 诸如此类,都为整型 2)浮点型(float) example: f = 1.1 f = -1 ...

  4. INSPIRED启示录 读书笔记 - 第34章 恐惧、贪婪、欲望

    消费者购买产品大多源于情感需求 企业级消费者出于恐惧和贪婪购买产品:如果不买这款产品,竞争对手会超过我,黑客会攻破我的防火墙,客户将弃我而去:如果买了,会赚得更多,省得更多 大众消费者购买产品的原因更 ...

  5. Docker Compose yml

    Wordpress + Mysql version: '3' services: db: image: mysql:latest volumes: - db_data:/var/lib/mysql e ...

  6. /var/spool/clientmqueue 爆满问题

    当你使用简单的sendmail发邮件的时候,或者系统默认要发一些邮件(比如cron发的邮件)的时候,首先会把邮件拷贝到这个目录里,然后等待MTA(mail transfer agent) 来处理,MT ...

  7. linux 基本命令___0001

    参考公众号:生信媛 参考链接:每天一个linux命令(61):wget命令 参考链接:<sort帮你排序>-linux命令五分钟系列之二十六 参考链接:每天一个linux命令(34):du ...

  8. Python httpServer服务器(初级)

    使用原生的python开发的web服务器,入门级! #!/usr/bin/python # -*- coding: UTF-8 -*- import os #Python的标准库中的os模块包含普遍的 ...

  9. Oracle给不同组数据添加顺序

    SELECT DENSE_RANK() OVER(ORDER BY TABLESPACE_NAME),T.* FROM USER_TABLES T;

  10. Phoenix性能优化

    目录: 1.建表优化 2.二级索引 3.并行处理 1.建表优化 Salting 翻译成中文是加盐的意思,本质是在hbase的rowkey的byte数组的第一个字节位置设定一个系统生成的byte值, 这 ...