标签:
常用函数:
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; namespace DIDAO.Common { public class CommonHelper { /// <summary> /// 传入虚拟路径 返回全路径的html字符串 /// </summary> /// <param name="context">当前数据上下文</param> /// <param name="virtualPath">虚拟路径</param> /// <returns>返回全路径的html字符串</returns> public static string GetHtmlFromVirtualPath(HttpContext context,string virtualPath) { string fileFullPath = context.Server.MapPath(virtualPath); string html = File.ReadAllText(fileFullPath); return html; } /// <summary> /// 输出错误信息到错误界面 /// </summary> /// <param name="context">当前数据上下文</param> /// <param name="virtualPath">错误页面的 虚拟路径</param> /// <param name="errormsg">错误信息</param> public static void OutputError(HttpContext context, string virtualPath,string errormsg) { string htmlError = GetHtmlFromVirtualPath(context, virtualPath); string htmlNewError = htmlError.Replace("{errormsg}", errormsg); context.Response.Write(htmlNewError); } /// <summary> /// 加密 /// </summary> /// <param name="str">原始密码</param> /// <returns>加密密码</returns> public static string Md5Encode(string str) { byte[] bytes = System.Text.Encoding.Default.GetBytes(str); MD5 md5 = new MD5CryptoServiceProvider(); byte[] newBytes = md5.ComputeHash(bytes); string newStr = BitConverter.ToString(newBytes).Replace("-",""); return newStr; } } }
有关Razor引擎进行cshtml页面解析的函数:
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 DIDAO.Common { public class RazorHelper { /// <summary> /// Razor解析cshtml页面,并输出到浏览器 /// </summary> /// <param name="context">上下文</param> /// <param name="cshtmlVirtualPath">cshtml页面的虚拟路径</param> /// <param name="model">传递的虚拟实例,可不传</param> public static void RazorParse(HttpContext context, string cshtmlVirtualPath, object model=null) { string fullPath = context.Server.MapPath(cshtmlVirtualPath); string cshtml = File.ReadAllText(fullPath); string cacheName = fullPath + File.GetLastWriteTime(fullPath); string html = Razor.Parse(cshtml, model, 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); } } }
有关Ajax异步请求的函数:
AjaxHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Script.Serialization; namespace DIDAO.Common { public class AjaxHelper { /// <summary> /// Ajax返回json数据 /// </summary> /// <param name="context">上下文</param> /// <param name="status">状态码:ok,error</param> /// <param name="msg">信息</param> /// <param name="data">数据:默认null</param> public static void WriteJson(HttpContext context,string status,string msg,object data=null) { string json = new JavaScriptSerializer().Serialize(new { status = status, msg = msg, data = data }); context.Response.Write(json); } } }
有关项目中变量类型的转换和验证:
VolidHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Script.Serialization; namespace DIDAO.Common { public class AjaxHelper { /// <summary> /// Ajax返回json数据 /// </summary> /// <param name="context">上下文</param> /// <param name="status">状态码:ok,error</param> /// <param name="msg">信息</param> /// <param name="data">数据:默认null</param> public static void WriteJson(HttpContext context,string status,string msg,object data=null) { string json = new JavaScriptSerializer().Serialize(new { status = status, msg = msg, data = data }); context.Response.Write(json); } } }
标签:
原文地址:http://www.cnblogs.com/adolphyang/p/4817448.html