/// <summary> /// 附加文件目录 /// </summary> public void AttachFiles(string basePath) { foreach (KeyValuePair <string, string> item in Attachs) { string source = TemplatePath.Remove(TemplatePath.LastIndexOf(@"\")) + @"\" + item.Key; string target = basePath.EndsWith(@"\") ? basePath : (basePath + @"\") + item.Value; IOHelper.CopyDir(source, target); } }
/// <summary> /// 实例化控件 /// </summary> /// <param name="ctl"></param> /// <param name="templateHtml"></param> /// <param name="dic"></param> /// <returns></returns> private Control InstanceControl(Control ctl, string templateHtml, Dictionary <string, templetaInfo> dic) { if (dic != null && dic.Count != 0) { foreach (var item in dic) { //System.Diagnostics.Stopwatch swtime = new System.Diagnostics.Stopwatch(); //swtime.Start(); #region 处理过程 try { //LoadControl(LoadControl(item.Key).GetType(), item.Value.Parameter); Type t = LoadControl(item.Key).GetType(); object instance = t.Assembly.CreateInstance(t.FullName); foreach (var field in item.Value.Parameter) //遍历字段赋值 { FieldInfo fieldinfo = t.GetField(field.Key, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.SetField); //取得字段信息 if (fieldinfo != null) { object c = Convert.ChangeType(field.Value, fieldinfo.FieldType); //动态转换类型 fieldinfo.SetValue(instance, c); //赋值 } } } catch (Exception ex) { item.Value.Error = ex.Message; //错误消息 ErrorDic.Add(item.Key, item.Value); //添加错误部件字典 We7.Framework.LogHelper.WriteLog(this.GetType(), ex); } #endregion //swtime.Stop(); //We7.Framework.LogHelper.WriteFileLog("showpageTimeTest.txt", "部件加载时间测试", "部件路径:" + item.Key + "\n部件运行加载时间:" + swtime.ElapsedMilliseconds.ToString() + "毫秒"); } if (ErrorDic == null || ErrorDic.Count == 0) //如果没有发生错误,则走正常流程 { ctl = LoadControl(TemplatePath); } else //错误处理流程 { string ErroAscx = string.Empty; foreach (var item in ErrorDic) { ErroAscx = Regex.Replace(templateHtml, ContentPattentn.Replace("{0}", item.Value.Wew).Replace("{1}", item.Value.Tagname).Replace("{2}", item.Value.Src), "<span style='color:Red' title='" + item.Value.Error + "'>此部件发生错误</span>"); //显示错误信息 ErroAscx = Regex.Replace(ErroAscx, RegisterPattetn.Replace("{0}", item.Value.Src).Replace("{1}", item.Value.Tagname).Replace("{2}", item.Value.Wew), string.Empty); //去掉错误部件注册信息 } string errorPath = TemplatePath.Insert(TemplatePath.LastIndexOf('.'), ".error");; //错误模板副本路径 if (AppCtx.Cache.RetrieveObject <string>(errorPath) != null && AppCtx.Cache.RetrieveObject <string>(errorPath).Equals(ErroAscx)) //如果有缓存 { ctl = LoadControl(errorPath); } else { FileHelper.WriteFileEx(Context.Server.MapPath(errorPath), ErroAscx, false); //写错误模板副本 AppCtx.Cache.AddObjectWithFileChange(errorPath, ErroAscx, new string[] { Context.Server.MapPath(errorPath) }); //添加缓存 ctl = LoadControl(errorPath); } } } return(ctl); }
/// <summary> /// 重写Render方法 /// </summary> /// <param name="writer"></param> protected override void Render(HtmlTextWriter writer) { if (!string.IsNullOrEmpty(Request["Createhtml"]) && Request["Createhtml"] == "1") { StringWriter strWriter = new StringWriter(); HtmlTextWriter tempWriter = new HtmlTextWriter(strWriter); try { base.Render(tempWriter); } catch (Exception ex) { strWriter.Write(""); }; //读取原始模板内容 HtmlDocument doc = new HtmlDocument(); doc.OptionAutoCloseOnEnd = true; doc.OptionCheckSyntax = true; doc.OptionOutputOriginalCase = true; try { doc.Load(Server.MapPath(TemplatePath), Encoding.UTF8); } catch { throw new Exception("格式化HTML错误"); } string strContent = doc.DocumentNode.InnerText; //提取控件注册信息 string pat = @"<%@[^>]*>"; Regex reg = new Regex(pat); MatchCollection m = reg.Matches(strContent); StringBuilder sb = new StringBuilder(); for (int i = 0; i < m.Count; i++) { string temp = m[i].Value; if (!m[i].ToString().Contains("Src=\"/")) { temp = m[i].Value.Replace("Src=\"", "Src=\"" + TemplatePath.Remove(TemplatePath.LastIndexOf("/")) + "/"); } sb.Append(temp + "\r\n"); } //给生成后模板添加控件注册信息 string content = strWriter.ToString(); string pat1 = @"<html[^>]*>"; string RegAndHtml = sb.ToString() + "\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">"; content = Regex.Replace(content, pat1, RegAndHtml, RegexOptions.IgnoreCase); content = content.Replace("<head>", "<head runat=\"server\">"); writer.Write(content); string channelUrl = ChannelHelper.GetChannelUrlFromUrl(Context.Request.RawUrl, Context.Request.ApplicationPath); string fileName = ""; if (string.IsNullOrEmpty(ColumnMode)) { fileName = "index.ascx"; } else { fileName = ColumnMode + ".ascx"; } string resultPath = Server.MapPath(TemplatePath.Remove(TemplatePath.LastIndexOf("/")) + "/HtmlTemplate/" + channelUrl + fileName); FileHelper.WriteFileEx(resultPath, content, false); } else { base.Render(writer); } }