sitemap介绍:
24
2013
09
17
2013
09
.Net时间计算函数,统计某一天是一年的第几周,这一周从哪天开始到哪天结束
Code:
/// <summary> /// 计算某年第一周的天数 /// </summary> /// <param name="dt">某年中的一个时间</param> /// <returns></returns> public static int DaysInFirstweekInYear(DateTime dt) { DateTime FirstDate = Convert.ToDateTime(string.Format("{0}-1-1", dt.Year)); int DayOfWeekInYear = DayOfWeek(FirstDate); int DaysInFirstWeek = 8 - DayOfWeekInYear; return DaysInFirstWeek; } /// <summary> /// 一年中的第几周 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static int WeekOfYear(DateTime dt)
17
2013
09
子站点间使用同一个cookie的问题
//c# code
public string testcookie(string id) { HttpCookie Cookie; Cookie = Request.Cookies["testcookie"]; string GotoDomain = System.Configuration.ConfigurationManager.AppSettings["GotoDomain"].ToString(); if (string.IsNullOrEmpty(id))//读取 { string test1 = ""; if (Cookie != null) { test1 = Common.OperateText.Decrypt(Cookie["test1"].ToString()); } return ("{\"读取到的test1\":\"" + test1 + "\",\"code\":\"200\"}"); } else//写入 { Cookie = new HttpCookie("testcookie"); string test1 = id + "-" + DateTime.Now + GotoDomain; Cookie["test1"] = Common.OperateText.Encrypt(test1); Cookie.Domain = GotoDomain; Cookie.Path = "/"; Cookie.Secure = false; Cookie.Expires = DateTime.Now.AddHours(24); Response.Cookies.Add(Cookie); return ("{\"写入的test1\":\"" + test1 + "\",\"code\":\"200\"}"); } }
06
2013
09
MetadataType的使用,MVC的Model层数据验证
//ActivityFlowContent是实体模型的model类
//ActivityFlowContentMetadata是自己写的model类
//这两个类属性相同可以形成映射关系,ActivityFlowContent中还可以加其他额外字段
[MetadataType(typeof(ActivityFlowContentMetadata))] public partial class ActivityFlowContent : IBaseEntity { [Display(Name = "活动流程分类")] public string ActivityClassificIDOld { get; set; } #region 自定义属性 #endregion } public class ActivityFlowContentMetadata { [ScaffoldColumn(false)] [Display(Name = "主键", Order = 1)] public string ID { get; set; } [ScaffoldColumn(true)] [StringLength(36, ErrorMessage = "长度不可超过36")] [Display(Name = "活动分类ID", Order = 2)] public object ActivityClassificID { get; set; } [ScaffoldColumn(true)] [StringLength(200, ErrorMessage = "长度不可超过200")] [Display(Name = "活动摘要", Order = 3)] public object Sumary { get; set; } [ScaffoldColumn(true)] [DataType(DataType.MultilineText,ErrorMessage="字符格式不正确")] [Display(Name = "活动内容", Order = 4)] public object Content { get; set; } [ScaffoldColumn(true)] [StringLength(50, ErrorMessage = "长度不可超过50")] [Display(Name = "关键字", Order = 5)] public object Keywords { get; set; } }
23
2013
08
22
2013
08
.net mvc中上传图片生成缩略图
Code:
#region CreateThumbnail /// <summary> /// 生成缩略图 /// </summary> /// <param name="uploadObject">上传的HttpPostedFile或图片物理路径</param> /// <param name="uploaddir">上传文件夹相对路径</param> /// <param name="ext">后缀(如:.jpg)</param> /// <param name="t_width">缩略图宽</param> /// <param name="t_height">缩略图高</param> /// <param name="filename">文件夹,不含路径和后缀</param> /// <param name="tm">枚举类-缩略图的样式</param> /// <returns>返回生成图片的路径</returns> public static string CreateThumbnail(object uploadObject, string uploaddir, string ext, int t_width, int t_height, string filename, ThumbModel tm) { System.Drawing.Image thumbnail_image = null; System.Drawing.Image original_image = null; System.Drawing.Bitmap final_image = null; System.Drawing.Graphics graphic = null; MemoryStream ms = null;
09
2013
08
谈.net mvc开发中gzip压缩的应用
压缩view的内容,可加过滤器
public class GzipFilter : ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { string acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"]; if (String.IsNullOrEmpty(acceptEncoding)) return; var response = filterContext.HttpContext.Response; acceptEncoding = acceptEncoding.ToUpperInvariant(); if (acceptEncoding.Contains("GZIP")) { response.AppendHeader("Content-Encoding", "gzip"); response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); } else if (acceptEncoding.Contains("DEFLATE")) { response.AppendHeader("Content-Encoding", "deflate"); response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); } } }
01
2013
08
学习并使用了两种linq to entity 的实现sql关键字in的查询方法
//构造Lambda语句
private static Expression<Func<TElement, bool>> BuildWhereInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values) { ParameterExpression p = propertySelector.Parameters.Single(); if (!values.Any()) return e => false; var equals = values.Select(value => (Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue)))); var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal)); return Expression.Lambda<Func<TElement, bool>>(body, p); }
20
2013
07
C#中npoi操作Excel[版本2.0.1读写2003、2007格式]
2.0.1npoi的dll文件下载:NPOI2.0.1.zip
public static void test1()
{
NpoiHelper np = new NpoiHelper();
DataTable dt1 = np.ReadExcel(AppDomain.CurrentDomain.BaseDirectory + "1测试数据.xls", 2).Tables[0];//读2003格式数据
DataSet ds1 = new DataSet();
ds1.Tables.Add(dt1.Copy());
ds1.AcceptChanges();
string SaveFileName = "output1.xls";
np.CreateExcel2003(SaveFileName, "sheet001", ds1, 0);//写2003格式数据
14
2013
07
使用LitJSON.dll处理json格式的数据示例
using LitJson;
//可到 http://sourceforge.net/projects/litjson/?source=dlp 下载
protected void Page_Load(object sender, EventArgs e)
{
String str = "{'name':'j1','id':1,'items':[{'itemid':1001,'itemname':'it1'},{'itemid':1002,'itemname':'it2'}]}";
//*** 读取JSON字符串中的数据 *******************************
JsonData jd = JsonMapper.ToObject(str);
String name = (String)jd["name"];
int id = (int)(jd["id"]);
Response.Write(string.Format("name:{0},id:{1}<br/>",name,id));
JsonData jdItems = jd["items"];
}