RazorHelper.cs

完整版 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="name">name属性的值</param>
        /// <param name="id">id属性的值</param>
        /// <param name="isCheck">是否选中</param>
        /// <returns>CheckBox标签</returns>
        public static RawString CheckBox(string name, string id, bool isCheck)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=‘checkbox‘ name=‘").Append(name).Append("‘ id=‘").Append(id).Append("‘ ");
            if(isCheck)
            {
                sb.Append("checked");
            }
            sb.AppendLine(" />");
            return new RawString(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 拼接扩展属性
            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
            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="namePropName">name属性的命名</param>
        /// <returns>RadioButtonList 标签</returns>
        public static RawString RadioButtonList(IEnumerable list,string valuePropName,string textPropName,object selectedValue,string namePropName)
        {
            //<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\" name=\"").Append(namePropName).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="namePropName">name属性的命名</param>
        /// <returns>CheckBoxList 标签</returns>
        public static RawString CheckBoxList(IEnumerable list, string valuePropName, string textPropName, object[] selectedValues, string namePropName)
        {
            //<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\" name=\"").Append(namePropName).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);
        }
    }
}
时间: 2024-08-18 06:59:53

RazorHelper.cs的相关文章

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

---恢复内容开始--- RazorEngine模板引擎大大的帮助了我们简化字符串的拼接与方法的调用,开源之后,现在在简单的web程序,winform程序,甚至控制台程序都可以利用它来完成. 但如何在使用中调用方法和使用自定义模板呢?来看这样一个例子 1 string str="hello @Model.Name"; 2 string parse=Razor.Prase(str,new {Name="Tom"}); 3 Console.WriteLine(parse

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

常用函数: CommonHelper.cs using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Web; using RazorEngine; using RazorEngine.Text

CS文件类头注释

1.修改unity生成CS文件的模板(模板位置:Unity\Editor\Data\Resources\ScriptTemplates 文件名:81-C# Script-NewBehaviourScript.cs) 本人将模板修改为如下图(红框内的内容) 备注:在"#"之间的为可替换的参数 2.修改模板可替换参数,在工程项目Asset文件夹在创建Editor文件 在文件夹下添加AddFileHeadComment.cs文件 内容如下 参数内容根据个人需求修改

CS 和 BS 的区别和优缺点

bs是浏览器(browser)和服务器(server) cs是静态客户端程序(client)和服务器(server) 区别在于,虽然同样是通过一个程序连接到服务器进行网络通讯,但是bs结构的,客户端运行在浏览器里,比如你看百度,就是通过浏览器.还有一些bs结构的应用,比如中国电信,以及一些电子商务平台.用bs结构的好处是,不必专门开发一个客户端界面,可用asp,php,jsp等比较快速开发web应用的程序开发. cs结构的,要做一个客户端.网络游戏基本上大多是cs结构,比如你玩传奇,要专门开个传

微软SQLHelper.cs类 中文版

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Collections; namespace LiuYanBanT { public class SqlHelper

AssemblyInfo.cs文件详解

一.前言 .net工程的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件.但我们实际上通过另一个形式操作该文件.那就是通过在鼠标右键点击项目的属性进入“应用程序”->“程序集信息”,然后修改信息. 二.作用 通过特性(Attribute)来设置程序集(dll文件)的常规信息,供查看或作为配置信息供程序内部使用. 三.详解 // 程序集标题 [assembly:AssemblyTitle("程序集标题")] // 程

全局程序集GlobalAssemblyInfo.cs进行版本控制(引)

原文出自:http://blog.csdn.net/oyi319/article/details/5753311 1.全局程序集GlobalAssemblyInfo.cs 我们编写的一个解决方案,通常会包含多个项目,而每个项目都有单独的程序集信息AssemblyInfo.cs.但是,你会发现一个问题,这些AssemblyInfo.cs当中有一部分在重复的,若能把它们提取出来放入一个单一文件中,修改AssemblyInfo中的诸如产品名.产品版本.版本等信息会变得轻松.那么,这个程序集信息文件,我

【141030】CS结构的VC++远程控制程序源代码

CS结构的VC++远程控制程序源代码,类似于pcAnywhere的程序,程序分为主服务端和主控端.主控端也就是客户端,由用户发送指令到服务端后来控制受控计算机.因为服务端是安装在受控机上的,其程序原理与著名的远程控制软件PcAnywhere非常相似,只是只完成了基本功能,有兴趣的可自己扩展程序吧. 客户端: 服务端: 完整源码下载地址:点击下载

《CS:APP》 chapter 6 The Memory Hierarchy笔记

The Memory Hierarchy 6.1 Storage Technologies The earliest IBM PCs didn't even have a hard disk. 让我惊奇的是早期的IBM直接没有硬盘... 6.1.1 Random-Access Memory Random-access memory(RAM) comes in two varieties- static anddynamic . Static RAM (SRAM) is faster and si