private void BuildView(string viewPath, XDocument doc) { ConvertCshtml _cs = new ConvertCshtml(_db); int addressId = _data.Id; int languageId = _languageId; var review = _cs.ReviewModel(addressId, languageId); review.JsSrc = @"/Areas/{0}/Views/_js/{1}-{2}.js".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()); review.CssHref = @"/Areas/{0}/Views/_css/{1}-{2}.css".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()); ContainConfigToCsproj(@"Areas\{0}\Views\_css\{1}-{2}.css".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()), _solutionDir, _projectName, doc); ContainConfigToCsproj(@"Areas\{0}\Views\_js\{1}-{2}.js".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()), _solutionDir, _projectName, doc); string jsPath = FileSugar.GetMapPath(review.JsSrc); string cssPath = FileSugar.GetMapPath(review.CssHref); if (FileSugar.IsExistFile(jsPath).IsFalse()) { FileSugar.CreateFile(jsPath); FileSugar.WriteFile(jsPath, "///<reference path=\"/_theme/tool/_reference.js\" />", "utf-8"); } if (FileSugar.IsExistFile(cssPath).IsFalse()) { FileSugar.CreateFile(cssPath); } var viewCode = GetTemplateCodeByName("view.cshtml", review); var languageSuffix = string.Empty; if (languageId != 1) { languageSuffix = $"_{GetLanguageList.Single(it => it.Id == languageId).Suffix}"; } FileSugar.WriteFile(viewPath, viewCode); ContainConfigToCsproj(@"Areas\{0}\Views\{1}\{2}{3}.cshtml".ToFormat(_data.AreaName, _data.ControllerName, _data.ActionName, languageSuffix), _solutionDir, _projectName, doc); }
public static void Filter(HttpContext context) { var localPath = context.Request.Url.LocalPath.ToLower(); var isViewsStaticFile = Regex.IsMatch(localPath, @"^/views/.*\.(js|css|jpg|png|gif|doc|docx|xls|xlsx|pdf|txt)$"); var isAreaViewsStaticFile = Regex.IsMatch(localPath, @"^/areas/\w+/views/.*\.(js|css|jpg|png|gif|doc|docx|xls|xlsx|pdf|txt)$"); if (isViewsStaticFile || isAreaViewsStaticFile) { string filePath = FileSugar.GetMapPath(localPath); var isExistFile = FileSugar.IsExistFile(filePath); if (isExistFile) { using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { long fileSize = fileStream.Length; byte[] fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); //如果不写fileStream.Close()语句,用户在下载过程中选择取消,将不能再次下载 fileStream.Close(); var fileExtension = FileSugar.GetExtension(filePath); context.Response.ContentType = fileExtension.Switch().Case(".css", "text/css").Case(".js", "text/js").Default("application/octet-stream").Break(); context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileSugar.GetFileName(filePath)); context.Response.AddHeader("Content-Length", fileSize.ToString()); context.Response.BinaryWrite(fileBuffer); context.Response.Flush(); context.Response.End(); } } } }
//public void RestoreLayoutElement(string htmlId, string key, int addressId, int languageId) //{ // var cshtmlPath = FileSugar.MergeUrl(PubUiEngineGet.GetUiEngineControlsDir, "layout", key + ".cshtml"); // var content = _.sysBest_PageContentService.GetSingle(it => it.LanguageId == languageId && it.AddressId == addressId); // var elementList = _.sysBest_ElementService.GetElementListByHtmlIdArray(new string[] { htmlId }); // content.Html = GetRestoreHtml(content.Html, elementList, htmlId); // _.sysBest_PageContentService.Update(content); //} /// <summary> /// 获取还原后的HTML /// </summary> /// <param name="html"></param> /// <param name="elementList"></param> /// <param name="htmlId"></param> /// <returns></returns> public string GetRestoreHtml(string html, List <SysBest_Element> elementList, string htmlId) { if (!elementList.Any(el => el.Html_Id == htmlId)) { return(html); } //根据htmlID查对应的元素 var ele = elementList.Single(el => el.Html_Id == htmlId); //正则匹配出对应的占位span标签 var span = Regex.Match(html, @"\<span.{1,30}?data\-id\=""" + htmlId + @""".*?\<\/span\>").Value; //正则匹配出控件类型 var categoryKey = Regex.Match(span, @"\<.*?data\-categorykey\=""(.+?)"".*?", RegexOptions.Singleline).Groups[1].Value; //正则匹配控件Key var key = Regex.Match(span, @"\<.*?data\-key\=""(.+?)"".*?").Groups[1].Value; var buildTemplatePath = FileSugar.MergeUrl(PubUiEngineGet.GetUiEngineControlsDir, categoryKey, "items", key, "build.cshtml"); if (FileSugar.IsExistFile(buildTemplatePath)) { BuildModel buildModel = new BuildModel(); buildModel.elementId = ele.Id; buildModel.addressId = ele.AddressId; var attrs = _db.Queryable <SysBest_ElementAttr>().Where(it => it.ElementId == ele.Id).ToList(); var events = _db.Queryable <SysBest_ElementEvent>().Where(it => it.ElementId == ele.Id).ToList(); buildModel.attrList = attrs.Select(attr => new KeyValuePair <string, string>(attr.Key, attr.Value)).ToDictionary(it => it.Key, it => it.Value); if (buildModel.attrList == null) { buildModel.attrList = new Dictionary <string, string>(); } buildModel.attrList.Add("name", ele.EleName); buildModel.eventList = new Dictionary <string, string>(); foreach (var eve in events) { List <EventActionTypeParas> pars = new List <EventActionTypeParas>(); if (eve.Pars.IsValuable()) { pars = eve.Pars.JsonToModel <List <EventActionTypeParas> >(); } string eveValue = GetEventActionHtml(eve.Value, pars, ele.EleName); buildModel.eventList.Add(eve.Key, eveValue); } var cshtml = FileSugar.FileToString(buildTemplatePath); buildModel.api = GetApiUrlByElement(ele); string finallyThisIsMyParsedTemplate = ""; finallyThisIsMyParsedTemplate = RazorEngineExtension.RazorPars(buildTemplatePath, cshtml, buildModel); var replaceHtml = finallyThisIsMyParsedTemplate; html = html.Replace(span, replaceHtml); } return(html); }
/// <summary> /// 生成controller /// </summary> private void BuildController(string controllerDir, string controllerPath, string controllerDomainPath, XDocument doc) { //写入控制器 if (!FileSugar.IsExistFile(controllerPath)) { FileSugar.CreateDirectory(controllerDir); ContainFileToCsproj(@"Areas\{0}\Controllers\{1}App\{1}Controller.cs".ToFormat(_data.AreaName, _data.ControllerName), _solutionDir, _projectName, doc); } //获取controller.cshtml的Model BuildControllerModel controllerModel = new BuildControllerModel() { areaName = _data.AreaName, controllerName = _data.ControllerName, actionName = _data.ActionName, siteName = ConfigSugar.GetAppString("siteName") }; var apiList = _db.Queryable <SysBest_DataApi>().ToList();//获取所有api int addressId = _data.Id; int languageId = _languageId; var eleList = _db.Queryable <SysBest_Element>().Where(it => it.AddressId == addressId && it.LanguageId == languageId).ToList(); var eleApidList = eleList.Where(it => it.ApiId != null && it.ApiId > 0).Select(it => it.ApiId).Distinct().ToList();//获取所有元素 //获取元素表里面的数据接口 controllerModel.apiList = apiList.Join(eleApidList, api => api.Id, ele => ele, (api, ele) => api).ToList(); //获取属性表里面的数据接口 controllerModel.apiList.AddRange(GetDataApiWithEleEventByEleId(eleList)); controllerModel.apiList = controllerModel.apiList.Distinct().ToList(); string controllerCode = GetTemplateCodeByName("controller.cshtml", controllerModel); FileSugar.WriteFile(controllerPath, controllerCode); //写入Domain if (!FileSugar.IsExistFile(controllerDomainPath)) { var domainCode = GetTemplateCodeByName("controller_domain.cshtml", new { controllerName = _data.ControllerName, areaName = _data.AreaName }); FileSugar.WriteFile(controllerDomainPath, domainCode); ContainFileToCsproj(@"Areas\{0}\Controllers\{1}App\Domain\{1}Domain.cs".ToFormat(_data.AreaName, _data.ControllerName), _solutionDir, _projectName, doc); } }
/// <summary> /// 写入日志文件 /// </summary> /// <param name="ex"></param> public static void WirteExp(Exception ex) { try { var logPath = FileSugar.MergeUrl( FileSugar.GetMapPath("~/"), "log", DateTime.Now.ToString("yyyy-MM-dd") + ".txt" ); if (FileSugar.IsExistFile(logPath).IsFalse()) { FileSugar.CreateFile(logPath); } FileSugar.AppendText(logPath, "***********{0}{1}***********".ToFormat("开始:", DateTime.Now)); FileSugar.AppendText(logPath, ex.Message); FileSugar.AppendText(logPath, "***********{0}***********\r\n".ToFormat("结束")); } catch { } }